// 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/