我尝试使用body-parser执行POST请求,如下所示:
var app = require('express')();
var bodyParser = require('body-parser');
var port = 1200;
console.log('Server is up at ' + port);
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(function (req, res, next) {
console.log('app.use entered...');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/msgboard', function(req, res) {
console.log('app.get entered...');
res.json(messages);
});
app.post('/message', function (req, res) {
console.log(req.body);
res.sendStatus(200);
});
app.listen(port);
但只是安慰{}。如果我没有为body-parser指定类型,则返回语法错误。