如何获得快递js的邮寄请求?
我试图得到一个小的服务,我写了后variables,但似乎无法得到它在我的app.post方法的请求variables。 我相信这是我处理请求的方式。 是否有额外的步骤,我必须采取处理的要求? 我也尝试使用express.bodyParser(),但我有一个错误,说这是不赞成。 以下是我的小node.js文件:
var express = require('express'); var app = express(); app.use(express.json()); // posting method : curl -X POST http://localhost:8080/post-page -d name=ArrowKneeous // to look at the req I found it better to start using: // nohup node testPost.js > output.log & app.post('/post-page',function(req,res){ var name= req.body.name; //undefined console.log('name is :' + name); //object console.log('req is: '+req); //object console.log('req.body is: ' +req.body); //undefined console.log('req.body.name is: '+req.body.name); //undefined console.log('req.params[0]: '+req.params[0]); //undefined console.log('req.query.name is: '+req.query.name); //empty brackets console.dir(req.body); //huge console.dir(req); //got stuff is replied to the curl command res.send('got stuff'); }); app.listen(8080);
你有
app.use(express.json());
处理一个JSON文章,但POSTing标准的URL编码forms的数据。
-d name = ArrowKneeous
您可能需要发布JSON
-d '{"name": "ArrowKneeous"}' -H "Content-Type: application/json"
或者你需要告诉express也接受URL编码的POST数据。
app.use(express.urlencoded());
编辑
这适用于Express 3.x
它应该和4.x
差不多,但是你需要加载body-parser
模块:
var bodyParser = require('body-parser'); app.use(bodyParser.json()); // OR app.use(bodyParser.urlencoded());