First write a Nodejs file to send the JSON data to the server: jsonclient.js:
var http = require (' http ');
var crypto = require (' crypto ');//Introducing cryptographic module var text = "phonenumber=15210943874&key=99999999";
var hasher=crypto.createhash ("MD5");//MD5 encryption Hasher.update (text);
var md5key=hasher.digest (' hex ');//hashmsg the data after the encryption Console.log ("The MD5 is:", Md5key);
var user = {phonenumber: ' 15210943874 ', authkey:md5key};
var userjson=json.stringify (user); varuserstring= ' data= ' +userjson;//this format resolves to req.body.datda var userstring = json.stringify (user) When parsing on the server side; Convert to JSON character format, req.body var headers = {' Content-type ': ' Application/json ',//If using varuserstring= ' Data= ' +us
The Erjson format should be ' Content-type ': set to ' application/x-www-form-urlencoded '//form form format ' content-length ': userstring.length}; var options = {host: ' localhost ',//HOST: Remember not to precede with http://port:3000,//port number path: '/image ',//route method: ' POST
',//submission mode headers:headers}; var req = http.request (options, function (res) {//Console.log (' STATUS: ' + res.statuscode);//Console.loG (' HEADERS: ' + json.stringify (res.headers));
Res.setencoding (' UTF8 ');
Res.on (' Data ', function (message) {var ret= eval (' + message + ') ');
Console.log (' response: ', ret);
});
});
Req.on (' Error ', function (e) {console.log (' problem with request: ' + e.message);}); Write data to request the body req.write (userstring);//writing to the Req.body req.end ();
Create a project with Express, and then add in the App.js file
App.post ('/image ', function (req, res, next) {
console.log (req.body);//request also has parameter Data,data value of a JSON string
//var Data= eval_r (' + Req.body.data + ');/need to convert JSON string to JSON object
//Console.log ("Data=" +data. PhoneNumber);
Console.log (Req.body.PhoneNumber);//parse JSON format data
res.contenttype (' json ');//Return Data type
res.send ( Json.stringify ({status: "Success"}))//Return a JSON-formatted data to the client
res.end ();
});