带有express.js和节点0.10.29的未定义的帖子

时间:2014-07-04 10:20:04

标签: javascript node.js http post express

我试图将json发布到快速服务器并在req.body.name上获取未定义。

配置如下所示:

var express = require('express');
var app = express();

app.configure(function(){
   app.use(express.json());
   app.use(express.urlencoded());
   app.use(express.methodOverride());
   app.use(app.router);
});

路线如下:

module.exports.start = function(){

  app.get('/', function(req, res){
      console.log("params: " + req.params);
      res.send("got it");
  });

  app.post('/test', function(req, res){
      console.log("Post test: " + req.body.name);//this is undefined
      res.send(req.body);
  });

  app.set('port', process.env.Port || 3000);
  app.listen(app.get('port'), function(){
      if (process.env.NODE_ENV == 'production') {
          console.log("listening on port: " + app.get('port'));
      }
      console.log("listening on port: " + app.get('port'));
  });

}

1 个答案:

答案 0 :(得分:0)

您还需要一个身体解析器

app.use(express.bodyParser());

现在,使用 expressjs 的版本4. *,a separate module.