node.js - 节点 ( Express ) 文件导入控制器
问题描述
在我的快速应用程序中,我有一个methods
包含methods.js
文件的目录,我在其中编写了需要在每个控制器中使用的非常常见的函数。
方法.js
function helloworld() {
return "Hello World"
}
现在我需要将文件添加到我的控制器文件中,并且我需要使用该功能..
我努力了
const Methods = require('../methods/methods')
exports.passengerStatus = (req, res) => {
let x = Methods.helloworld()
console.log(x)
}
正在调用路线,但错误是
Methods.helloworld() is not define
如何将文件导入控制器?有什么方法可以导入文件,这样我就可以在不导入控制器的情况下访问文件方法。
解决方案
您可以编写methods.js如下
var commonFunctions = {};
commonFunctions.sample = function(){
// Write your code here
};
// Add other functions as sample here
module.exports = commonFunctions;