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

标签 node.js

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

相关文章:

javascript - 无法使用 Facebook 护照成功重定向

android - 如何使用 Firebase Cloud 消息发送群组通知?

ruby-on-rails - https 网站上的主宰

javascript - 运行 Node.js 服务器时出现问题

javascript - @babel/plugin-proposal-class-properties 不起作用

node.js - javascript 函数设置的表单字段值的 req.body 值未定义

android - GCM 注册设备接收两种类型的通知

Visual Studio 12 中不显示 Node.js 交互窗口

node.js - Node js 邮件喷射方法链接 stub

node.js - 在高负载下,Node.js 的可伸缩性是否会因为垃圾收集而受到影响?