Details about how Node. Js processes post data, and details about node. jspost
Implementation
Put the callback functions of the data and end events directly on the server, and collect all POST data in the data Event Callback. After receiving all data and triggering the end event, the callback function calls the request route and passes the data to it. Then, the request route passes the data to the request processing program.
Steps
Step 1 we set the encoding format of the received data to UTF-8, step 2 registers the listener for the "data" event to collect new data blocks each time we receive, assign the value to the postData variable. In the last step, we move the request route call to the end event handler to ensure that it is triggered only after all data is received, trigger 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.start(router.route,handle);
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(200, {"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(); } } exports.route=route;
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/
Result:
Enter "I LOVE YOU" in the text box and click Submit.
Use the querystring module to extract only the text. Modify requestHandlers. js to only return the text.
var querystring = require("querystring"); function start(response,postData) { console.log("Request handler 'start' was called."); var body = '
Restart, enter I LOVE YOU, and submit
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message.