【发布时间】:2014-01-27 01:04:40
【问题描述】:
我正在尝试在我编写的一个小服务中获取发布变量,但似乎无法从我的 app.post 方法中的请求变量中获取它。我相信这是我处理请求的某种方式。我必须采取其他步骤来处理请求吗?我也尝试过使用 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);
【问题讨论】: