在restify 1.xx中如何访问数据?

我将数据发布到restify API,但找不到任何有关如何访问发布数据的当前示例。 这个怎么用?

我find了答案。 包含的插件之一需要被激活, restify.bodyParser 。 这些数据可以在req.params (默认)或req.bodymapParams: false )中find,具体取决于设置 (具体看BodyParser部分)。

例:

 server.use(restify.bodyParser({ mapParams: false })); // mapped in req.body 

要么:

 server.use(restify.bodyParser()); // mapped in req.params 

很简单:

 server.use(restify.bodyParser({ mapParams: false })); 

你需要在restify中激活bodyParser

对于restify 5.0.0+ ,请使用:

 server.use(restify.plugins.bodyParser()); 

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

对于旧版本使用:

 server.use(restify.bodyParser()); 

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

 server.post('/article', (req, res, next) => { console.log(req.body) next() }) 

这段代码将把请求主体打印到控制台上:

 var restify = require('restify'); var server = restify.createServer(); // This line MUST appear before any route declaration such as the one below server.use(restify.bodyParser()); server.post('/customer/:id', function (req, resp, next) { console.log("The request body is " + req.body); response.send("post received for customer " + req.params.id + ". Thanks!"); return next(); });