S F*_*Faz -1 javascript node.js node-modules
得到了一个我无法解决的非常简单的问题。我无法从Node.js中的另一个JavaScript文件调用函数。
我的主文件:
var Binance = require('./binance.js');
//later in the file
app.post('/createBotOrder', function (req, res, next) {
console.log("in");
console.log(Binance);
console.log(Binance.sum(1, 2));
console.log("in 2");
});
Run Code Online (Sandbox Code Playgroud)
我的辅助文件:
module.exports = function() {
this.sum = function(a,b) { return a+b };
this.multiply = function(a,b) { return a*b };
//etc
}
Run Code Online (Sandbox Code Playgroud)
输出:
in
[Function]
Run Code Online (Sandbox Code Playgroud)
您要导出为具有功能的对象作为对象。
module.exports = { myFunc : function () { //stuff } }
Run Code Online (Sandbox Code Playgroud)
然后在导入myFunc的文件中
const Binance = require('./binance.js')
Binance.myFunc()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |