gpt4 book ai didi

node.js - Node (Express)文件导入到 Controller 中

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:05 26 4
gpt4 key购买 nike

在我的 Express 应用程序中,我有一个 methods 目录,其中包含 methods.js 文件,我在其中编写需要在每个 Controller 中使用的非常常见的函数。

方法.js

function helloworld() {
return "Hello World"
}

现在我需要将文件添加到我的 Controller 文件中,并且我需要使用该函数..

我已经尝试过

const Methods = require('../methods/methods')

exports.passengerStatus = (req, res) => {
let x = Methods.helloworld()
console.log(x)
}

正在调用路由,但出现错误

 Methods.helloworld() is not define

如何将文件导入 Controller ?有什么方法可以导入文件,以便我可以访问文件方法而无需导入 Controller 。

最佳答案

你可以如下编写methods.js

var commonFunctions = {};

commonFunctions.sample = function(){
// Write your code here
};

// Add other functions as sample here


module.exports = commonFunctions;

关于node.js - Node (Express)文件导入到 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59979661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com