gpt4 book ai didi

node.js - 向 module.exports 添加更多功能

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

// index.js //
module.exports = (req, res) => {
res.send("Hello World!");
};

当我需要文件时调用此函数。

let index = require("./index.js");
app.get("/", index);

我可以向 module.exports 添加一些其他函数吗?

像这样:

// index.js //
module.exports = (req, res) => {
res.send("Hello World!");
},
hi: (req, res) => {
res.send("Hi!");
};
let index = require("./index.js");
app.get("/", index);
app.get("/hi", index.hi);

最佳答案

我建议你使用:

function index(req, res) {
res.send("Hello World!");
}

function hi(req, res) {
res.send("hi!");
}

module.exports = {
Index: index,
Hi: hi
}

然后引用您提到的:

let index = require("./index.js");
app.get("/", index.Index);
app.get("/hi", index.Hi);

关于node.js - 向 module.exports 添加更多功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619991/

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