A small back-end program today, to deal with json data, express frame I can not directly address the need for json extraction, the Internet to find a pile and found four kinds of parsing json format, look at this record
- www-form-urlencoded
- form-data
- application/json
- text/xml
There are four format handling, first add the Python modules:
1 var express = require('express'); 2 var app = express(); 3 var bodyParser = require('body-parser');
The process then different formats:
www-form-urlencoded:
app.use(bodyParser.urlencoded({ extended:true })); app.post('/urlencoded', function(req, res){ console.log(req.body); res.send(" post successfully!"); }); app.listen(3000);
from-data:
var multipart = require('connect-multiparty'); var multipartMiddleware = multipart(); app.post('/formdata',multipartMiddleware, function (req, res) { console.log(req.body); res.send("post successfully!"); });
application/json:
var express = require('express'); was app = Out (); var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.post('/urlencoded', function(req, res){ console.log(req.body); res.send(" post successfully!"); }); app.listen(3000);
text/xml:
var express = require('express'); var bodyParser = require('body-parser'); var xml2json=require('xml2json'); was app = Out (); app.use(bodyParser.urlencoded({ extended: true })); app.post('/xml', function (req, res) { req.rawBody = '' ; // add to receive variables was 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);
Note: I believe in micro small program is application / json post request of the successful interaction.
Thank you for this blog click on the link
Reproduced in: https: //www.cnblogs.com/elve960520/p/11011178.html