Detailed node.js how to handle post data _node.js

Source: Internet
Author: User

Realize the idea

The callback function of the data and end events is placed directly on the server. Collects all post data in the data event callback, receives all data, triggers the end event, invokes the request route for the callback function, passes the data to it, and then requests the route to pass the data to the request handler.

Implementation steps

In the first step, we set up the code format for receiving data for UTF-8, and the second step registers the listener for the "data" event, which collects the new block of data received each time and assigns it to the postdata variable, and in the last third step we move the call to the request route to the end event handler. To ensure that it is triggered only once all data has been received, and only once. We also pass the post data to the request route

Sample code

Index.js

var server = require ("./server"); 
var router=require ("./router"); 
var requesthandlers=require ("./requesthandlers"); 
 
var handle = {} 
handle["/" = Requesthandlers.start; 
handle["/start"] = Requesthandlers.start; 
handle["/upload"] = requesthandlers.upload; 
 

Server.js

var http = require ("http"); 
var url=require ("url"); 
 
function Start (route,handle) { 
 function onrequest (request, Response) { 
  var postdata= ""; 
    var pathname=url.parse (request.url). Pathname; 
  Console.log ("Request for" +pathname+ "received."); 
    
   Request.setencoding ("UTF8"); 
    
   Request.addlistener ("Data", function (postdatachunk) { 
     postdata + = Postdatachunk; 
     Console.log ("Received POST data chunk '" + 
     Postdatachunk + "'."); 
  }); 
 
  Request.addlistener ("End", function () { 
   route (handle, pathname, response, postdata); 
  }); 
    Route (Handle,pathname,response); 
   
  Response.writehead ({"Content-type": "Text/plain"}); 
  Response.Write ("This is a demo"); 
  Response.End (); 
 } 
 
 Http.createserver (ONrequest). Listen (5656, ' 127.0.0.1 '); 
 Console.log ("Server has started.") localhost:5656 "); 
} 
 
Exports.start = start;

Router.js

Function route (handle,pathname,response,postdata) { 
  Console.log ("About to route a request for" +pathname); 
  if (typeof handle[pathname]== ' function ') { 
    handle[pathname] (response,postdata); 
  } 
  else{ 
    Console.log ("No request handler found for" +pathname); 
    Response.writehead (404, {"Content-type": "Text/plain"}); 
  Response.Write ("404 Not Found"); 
  Response.End (); 
  } 
 

Requesthandlers.js

//var querystring = require ("querystring"); 
 
 function Start (response,postdata) {console.log ("Request handler ' start ' was called."); var body = '  
 

Run:node mynode/index

Browser inputhttp://localhost:5656/

Results:

Enter "I Love You" in the text box click Submit

Use the QueryString module to extract only text, modify Requesthandlers.js to return only text

 var querystring = require ("querystring"); 
 
 function Start (response,postdata) {console.log ("Request handler ' start ' was called."); var body = ' 

Reboot, still Enter I love you, submit

Summarize

The above is the entire content of this article, I hope that the content of this article for everyone's study or work to bring certain help, if you have questions you can message exchange.

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.