gpt4 book ai didi

javascript - Node.js Express 模块导出

转载 作者:搜寻专家 更新时间:2023-11-01 00:21:28 26 4
gpt4 key购买 nike

我收到这个错误

app.get is not a function

这是我的 config/express.js

var express = require('express');


module.exports = function(){

var app = express();

app.set('port',3000);

return app;

};

这是我的 server.js

var http = require ('http');

var app = require ('./config/express');


http.createServer(app).listen(app.get('port'), function(){

console.log("Express Server Runing on port"+ app.get('port'));


});

我做错了什么?

最佳答案

config/express.js 中导出一个函数,但将其用作应用程序。试试这个:

var express = require('express');
var app = express();
app.set('port', 3000);
module.exports = app;

编辑:但以下是首选:

/* config/express.js */
var express = require('express');
module.exports = function() {
var app = express();
app.set('port', 3000);
return app;
};


/* server.js */
var http = require('http');
var app = require('./config/express')(); // Notice the additional () here

http.createServer(app).listen(app.get('port'), function() {
console.log("Express Server Runing on port"+ app.get('port'));
});

关于javascript - Node.js Express 模块导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42205271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com