在以 (req,res) 作为参数的箭头函数上使用 module.exports

西尔维乌·斯蒂尔贝斯库

我目前正在开发一个带有节点的项目,我需要在该项目中使用 module.exports 来导出函数。我像这样导出 showMessage 函数:

   const  showMessage = (req, res) => {
        res.status(200).send(`Message: ${req.param.message}`);
    }

module.exports = {
    showMessage
}

并在此处导入:

const express = require('express');
const router = express.Router();
const  { showMessage } =  require('../controllers/auth');

router.get("/:message", showMessage);

module.exports = router;

在这里更进一步:

fs.readdirSync("./routes").map((r) => app.use("/api", require(`./routes/${r}`)));

我的项目应该从 api 获取消息并将其显示在网页上。例如:如果我访问 http://localhost:8000/api/good-morning,我应该进入我的网页: Message: good-morning 相反我得到 Message: undefined。这让我觉得 showMessage 函数中传递的 (req,res) 参数的范围有问题。

我尝试使用 import <> from <> 语法,但没有用

安德烈

它应该req.params代替req.param. 以下是 API 文档:https ://expressjs.com/en/4x/api.html#req.params

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章