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.