在我的快速应用程序中,我有一个包含methods.js
文件的methods
目录,我正在编写需要在每个控制器中使用的非常常见的函数。
方法.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
如何将文件导入控制器?并且有没有办法导入文件,以便我可以在不导入控制器的情况下访问文件方法。
你可以编写方法.js如下所示
var commonFunctions = {};
commonFunctions.sample = function(){
// Write your code here
};
// Add other functions as sample here
module.exports = commonFunctions;
你也可以这样写
方法.js
function helloWorld() {
return "hello method";
}
function mySecondMethod() {
return "hello my second method";
}
function myThirdMethod() {
return "hello my third method";
}
module.exports = {
helloWorld,
mySecondMethod,
myThirdMethod
}
婴儿车.js
let { helloWorld,mySecondMethod,myThirdMethod } = require("./method.js");
console.log(helloWorld);
console.log(mySecondMethod);
console.log(myThirdMethod);