一个人如何访问数据来解析1.xx?

JMH*_*bog 27 rest post node.js

我将数据发布到restify API,但找不到任何有关如何访问发布数据的示例.这是如何运作的?

JMH*_*bog 45

我找到了答案.其中一个插件需要激活, restify.bodyParser.然后可以在req.params(默认)或req.body(mapParams: false)中找到数据,具体取决于设置(具体来看BodyParser部分).

例:

server.use(restify.bodyParser({ mapParams: false })); // mapped in req.body
Run Code Online (Sandbox Code Playgroud)

要么:

server.use(restify.bodyParser()); // mapped in req.params
Run Code Online (Sandbox Code Playgroud)


Rap*_*nah 6

对于restify 5.0.0+,使用:

server.use(restify.plugins.bodyParser());
Run Code Online (Sandbox Code Playgroud)

https://github.com/restify/node-restify/issues/1394#issuecomment-312728341

对于旧版本,请使用:

server.use(restify.bodyParser());
Run Code Online (Sandbox Code Playgroud)

在告诉restify使用bodyParser中间件后,请求主体将在请求对象的body属性上可用:

server.post('/article', (req, res, next) => {
  console.log(req.body)
  next()
})
Run Code Online (Sandbox Code Playgroud)


dav*_*ver 5

很简单:

server.use(restify.bodyParser({ mapParams: false }));
Run Code Online (Sandbox Code Playgroud)

你需要在restify中激活bodyParser