app.js:
var app = express();
require('./other_file.js')(app);
other_file.js:
app.post('/test', function(req, res) {
console.log(true);
});
结果:应用程序未定义。
如何在require文件中使用express方法?
提前致谢。
最佳答案
other_file.js
应该是这样的:
module.exports = function(app) {
app.post('/test', function(req, res) {
console.log(true);
});
}
更长的故事:
当您require()
一个文件时,您会得到一个对象(exports
对象)。如果你想取回一个函数,你需要让你的函数替换导出对象。为此,您需要将函数分配给 module.exports
关于javascript - node.js(快速框架): require and post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326179/