您的位置:首页 > Web前端 > JavaScript

学习express之实现Json的Post请求

2015-09-08 00:00 543 查看
参考:http://yijiebuyi.com/blog/38f1437bf5b43fcf90e6529a81f258f1.html

参考:http://yijiebuyi.com/blog/90c1381bfe0efb94cf9df932147552be.html

1)引入模块body-parser

var bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));

// parse application/json
app.use(bodyParser.json());


2)post方法

router.post('/group', function(req, res, next) {
console.log('post group redpack');
console.log(req.headers['content-type'])
res.send(req.body);
});


3)注意事项

**bodyParser模块,应放在路由之前

var bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));

// parse application/json
app.use(bodyParser.json());

...

app.use('/', routes);
app.use('/app', apps);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  express post json