Client-submitted JSON (NODEJS) and server parse JSON and return JSON data (Express) __js

Source: Internet
Author: User
Tags md5 encryption string to json port number
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 ();
});


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.