JavaScript(Node.js):app.set('port',port); ^ TypeError:app.set不是一个函数

5

当我尝试运行一个简单的打印常见的“Hello World”程序时,出现了以下错误代码。

 app.set('port', port);
        ^

    TypeError: app.set is not a function
        at Object.<anonymous> (/home/xpuser/Desktop/Nodetest/bin/www:16:5)
        at Module._compile (module.js:409:26)
        at Object.Module._extensions..js (module.js:416:10)
        at Module.load (module.js:343:32)
        at Function.Module._load (module.js:300:12)
        at Function.Module.runMain (module.js:441:10)
        at startup (node.js:139:18)
        at node.js:968:3

写下 "module.exports = app;" 后出现了以下错误:
module.exports = app;
                  ^

ReferenceError: app is not defined
     at Object. <anonymous> (/home/xpuser/Desktop/Nodetest/app.js:23:18)
     at Module._compile (module.js: 409: 26)
     at Object.Module._extensions..js (module.js: 416: 10)
     at Module.Load (module.js: 343: 32)
     at Function.Module._load (module.js: 300: 12)
     at Module.require (module.js: 353: 17)
     at require (internal / module.js: 12: 17)
     at Object. <anonymous> (/ home / xpuser / Desktop / Nodetest / bin / www: 7: 11)
     at Module._compile (module.js: 409: 26)
     at Object.Module._extensions..js (module.js: 416: 10)

你在哪里定义了app - Rayon
我认为它定义在bin / www /中。 - DavidRT49
var app = require('../app'); 是吧? - DavidRT49
2个回答

17

我认为你正在尝试使用"Express"模块,所以你需要在你的文件中添加以下内容:

var express = require('express');
var app     = express();

然后您可以设置:

var port = "1000";
app.set('port', port);

module.exports = app;

14
如果你有 "app = require('../app')",那么请确保在 app.js 最底部加上以下代码: module.exports = app 这样当你在 /bin/www 中调用它时,实际上会获取到代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接