Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Restify解析url编码的正文_Node.js_Httprequest_Restify - Fatal编程技术网

Node.js 使用Restify解析url编码的正文

Node.js 使用Restify解析url编码的正文,node.js,httprequest,restify,Node.js,Httprequest,Restify,我无法使用restify向node.js API发送url编码的帖子。我的restify应用程序设置如下: app.use(restify.acceptParser(app.acceptable)); app.use(restify.queryParser()); app.use(restify.urlEncodedBodyPars

我无法使用restify向node.js API发送url编码的帖子。我的restify应用程序设置如下:

app.use(restify.acceptParser(app.acceptable));                                  
app.use(restify.queryParser());                                                 
app.use(restify.urlEncodedBodyParser());
但当我使用curl请求我的应用程序时,我会发出以下请求:

curl -X POST -H "Content-type: application/x-www-form-urlencoded" -d quantity=50 http://app:5000/feeds
我在视图中获得以下输入体:

console.log(req.body)  // "quantity=50"
提前感谢,


Mattias

Restify的默认设置将解析的参数放置在
req.params
中。这是由
queryParser
和不同的
bodyParser
中间件完成的

因此,要访问
quantity
参数,请使用
req.params.quantity

如果您确实想使用
req.body
,则需要将
mapParams:false
传递给
bodyParser
构造函数:

app.use(restify.plugins.urlEncodedBodyParser({ mapParams : false }));

现在
req.body
将包含解析的参数。

当我使用解析器时,我的整个服务对任何URL都没有响应。它永远不会返回,也不需要断点hit@ChristianBongiorno这通常表示中间件没有沿着中间件链传递请求。这个答案很老了,可能是后来发生了一些变化,这可能会导致你所看到的行为。我独立地发现了这一点,但你是对的。CORS模块阻止了整个过程。老实说,restify并没有给我留下深刻的印象。