Handling POST requests by nodejs

Source: Internet
Author: User

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:

 

 

 

 

 

 

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.