【问题标题】:restify's body parser is not working for some reasonrestify 的正文解析器由于某种原因无法正常工作
【发布时间】:2020-05-13 03:36:41
【问题描述】:

我不明白为什么会这样:

instance.use(restify.bodyParser());

但这不起作用:

instance.use((req, res, next) => {
  restify.bodyParser();
  next();
});

UPD:我试过restify.bodyParser()(req, res, next)。事实上,由于某种原因它不起作用。

restify.bodyParser() 导出函数数组:[read, parseBody]。所以使用restify.bodyParser()(...) 会抛出错误... is not a function

我仍在努力实现为什么会这样。

【问题讨论】:

    标签: node.js webserver middleware restify body-parser


    【解决方案1】:

    use 需要 argument of the form function (req, res, next) or an array of functions of this formrestify.bodyParser() 是这种形式的函数数组。因此,您需要将reqresnext 传递给数组中的每个函数。根据您要执行的操作,您可能希望返回一个中间件数组或按顺序调用这些中间件。

    【讨论】:

    • 妈的,就这么简单)谢谢
    • 我刚刚测试过了。由于某种原因,它不起作用。查看有问题的 UPD
    • 你说得对,use 也可以接受一系列中间件,没想到。我更新了我的答案,希望对您有所帮助。但是,确切的用法在很大程度上取决于我们的用例,所以我删除了代码示例。
    【解决方案2】:

    如果您来这里寻找相同错误消息“restify.bodyParser 不是函数”的答案...

    从restify版本5.*开始,restify的bodyParser函数应该是:

    restify.plugins.bodyParser()

    https://github.com/restify/node-restify/issues/1414

    【讨论】:

      【解决方案3】:

      在当前版本的 Restify 中,使用插件restify.plugins.bodyParser()

      【讨论】:

        猜你喜欢
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-07
        相关资源
        最近更新 更多