我正在尝试将函数响应从一个 Node.js 脚本传递到另一个
这是我正在尝试的。 索引.js
var express = require('express');
require('./meter');
var app = express();
app.get('/',function(req,res){
return; //hi function from another file
})
app.listen(3000);
console.log('listening on 3000 port')
meter.js
module.exports = {
hi: function(req, res) {
return res.status(200).json({"message" : "Hello V1"});
}
};
只需要函数就可以完成这项工作吗?
提前致谢。
最佳答案
当你使用require时,你应该把它赋值给一个变量,然后你就可以在你的代码中使用它了:
var express = require('express');
var meter = require('./meter'); // meter will be an object you can use later on
var app = express();
app.get('/',meter.hi); // you actually don't need another annonimous function, can use hi directly as the callback
app.listen(3000);
console.log('listening on 3000 port')
关于javascript - 将参数函数Node.js从一个js传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40560106/