简体   繁体   中英

Undefined Post with express.js and node 0.10.29

I'm trying to post json to an express server and getting undefined on req.body.name.

the configuration look like this:

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);
});

The routes look like this:

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'));
  });

}

You'll also need a body parser

app.use(express.bodyParser());

which is now, with the version 4.* of expressjs , a separate module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM