node. JS gets Get/post data

Source: Internet
Author: User

goal : Use node. js to build a simple server to provide a simple interface that achieves two digital subtraction calculations and returns JSON results. (General Get/post)

Knowledge points

    1. Get Get Data
    2. Get Post Data
    3. Understanding Listener Events

实现

var http = require ("http");            Provide the Web service var URL = require ("url");    Parse GET request var query = require ("querystring"); Parse POST request//service var server = function (request,response) {//define header Response.writehead (200,{"Content-type": "Text/json"}    );        The judgment is Get/post request if (Request.method = = "GET") {var params = [];        params = Url.parse (request.url,true). query;        params[' fruit '] = compute (params);        Response.Write (Json.stringify (params));    Response.End ();        }else{var postdata = "";        Request.addlistener ("Data", function (postchunk) {postdata + = Postchunk;            })//post End output Request.addlistener ("End", function () {var params = Query.parse (postdata);            params[' fruit '] = compute (params);            Response.Write (Json.stringify (params));        Response.End (); })}}//COMPUTE var compute = function (params) {switch (params[' type ')} {case "add": Return parsefloat (params[' num ')]) + parsefloat (params[' num1 ');        Case "Subtract": Return parsefloat (params[' num '))-parsefloat (params[' num1 ']);        Case "multiplication": Return parsefloat (params[' num ') * parsefloat (params[' num1 ']);    Case "Division": Return parsefloat (params[' num ')/parsefloat (params[' num1 ']);  }}//opens the service at 127.0.0.1:8080http.createserver (server). Listen (8080);   Console.log ("Server start!");

It is important to note that because the post quality is too large, the data cannot be taken directly as a GET request. It is necessary to increase the listener event and not end the reply (response) to complete the message flow until the end of the post, so put the output and end to the listener end event.

Test

Get:http://127.0.0.1:8080/?num=32&num1=13&type=add
print {"num": "+", "NUM1": "+", "type": "Add", "Fruit": 45}

POST:
I tested the post using the Chrome plugin postman, which, of course, was a test get request. It contains all request types (delete, put, copy, patch, lock, etc.)

node. JS gets Get/post data

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.