分别是这四种:express
www-form-urlencoded,json
form-data,服务器
application/json,app
text/xmlpost
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended:true })); app.post('/urlencoded', function(req, res){ console.log(req.body); res.send(" post successfully!"); }); app.listen(3000);
能够用postman进行测试,这里就不赘述。测试
var multipart = require('connect-multiparty'); var multipartMiddleware = multipart(); app.post('/formdata',multipartMiddleware, function (req, res) { console.log(req.body); res.send("post successfully!"); });
app.use(bodyParser.json());
var express = require('express'); var bodyParser = require('body-parser'); var xml2json=require('xml2json'); var app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.post('/xml', function (req, res) { req.rawBody = '';//添加接收变量 var json={}; req.setEncoding('utf8'); req.on('data', function(chunk) { req.rawBody += chunk; }); req.on('end', function() { json=xml2json.toJson(req.rawBody); res.send(JSON.stringify(json)); }); }); app.listen(3000);