私は現時点でexpressjsでWebアプリケーションを作成しています。私が実行した場合app.set変数にアクセスできません。
callingScript.js
const http = require('http');
const app = require('./module');
const port = process.env.PORT || 3000;
app.set('port', port);
const server = http.createServer(app);
// it serves to the correct port, so the problem is passing it to module.js
app.listen(port);
module.js
const express = require('express');
const app = express();
// this always logs `undefined`
console.log(app.get('port'));
module.exports = app;
:
私はアプリがmodule
にcallingScript
から実行されているポートを通過したいです上記のアプリケーション私はresuを見たい
$ node callingScript.js
3000
$ PORT=8080 bash -c 'node callingScript.js'
8080
ですが、代わりにundefined
が毎回印刷されます。
module.jsが呼び出し元のスクリプトから変数を渡されていないようです。
どうすれば2つの間でポートを通過できますか?
あなたは '.set()'を呼び出す前に '.get()'を呼び出しています。 – robertklep
@robertklepしかし、どうすればそのポートを通過できますか? – theonlygusti
'const port = process.env.PORT ||を宣言する3000; '' require 'の前に '' callingScript.js'モジュールの一番上にあります。 – alexmac