分別是這四種: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);