Handling POST requests by nodejs
The simulation below is the Web page login process. When we request a link, we will get a form, then fill in the corresponding values in the form, and then submit the login.
Var http = require ('http'); var querystring = require ('querystring'); http. createServer (function (request, response) {var responseString = ''; response. writeHead (200, {'content-type': 'text/html'}); // if it is a get request var postData = ""; if (request. method = "GET") {responseString ='\ \ \\\\\ '; Response. write (responseString); response. end ();} else if (request. method = "POST") {request. setEncoding ("utf8"); request. addListener ("data", function (postDataChunk) {postData + = postDataChunk;}); request. addListener ("end", function () {var objectPostData = querystring. parse (postData); for (var I in objectPostData) {responseString + = I + "=>" + objectPostData [I] +"
";}Response. write (responseString); response. end () ;}}}). listen (8080, '192. 168.33.98 ');
Open the browser and enter http: // 192.168.33.98: 8080/. 192.168.33.98 is the IP address of my computer. The webpage is displayed as follows:
This webpage is generated in the code that processes the GET request. By default, it is filled in with the corresponding values. You can modify or directly click the submit button to submit the webpage and then obtain the following results: