Home > Article > Web Front-end > Example of http form submission in Nodejs
This article mainly introduces the http form submission of Nodejs in detail, which has certain reference value. Interested friends can refer to
The request of the http module was introduced before The process of responding to also introduces the data transmission between the client and server of the TCP protocol. The http protocol is the upper layer protocol of TCP. Here we create a simple web server and process the submitted form data, as summarized in the great Node.js book.
POST method to submit form data
As I have summarized before, you need to use the POST method to submit data to the server, and the request information for the GET methodAll in the queryString, there is no request body, and the data transmitted by the POST method is all in the request body, so the POST method needs to be used when submitting form data.
req is the request information, req.url represents the requested address. After the server is running, the URL requested by req is 127.0.0.1:3000. At this time, req.url is '/', then return is a string of form data. The method set in the form data is post, action is '/url', the surface submission method is POST, and the address to submit the data is 127.0.0.1:3000/ url, and after submission, a new page needs to be obtained, which is 127.0.0.1:3000/url. At this time, req.url is '/url', so another page is displayed.
//提交表单数据 var http=require('http'); var querystring=require('querystring'); var server=http.createServer(function (req,res) { //req.url不同则返回的页面不同 if('/'==req.url){ res.writeHead(200,{'Content-Type':'text/html'}); res.write([ '<form method="post" action="/url">', '<h1>My Form</h1>', '<fieldset>', '<label>Personal Information</label>', '<p>What is your name?</p>', '<input type="text" name="name">', '<button>submit</button>', '</form>' ].join('')); res.end(); }else if('/url'==req.url&&req.method=='POST'){ var reqBody=''; req.on('data',function (data) { reqBody += data; }); req.on('end',function () {//用于数据接收完成后再获取 res.writeHead(200,{'Content-Type':'text/html'}); res.write('you have sent a '+req.method+' request\n'); res.write('<p>Content-Type:'+req.headers['content-type']+'</p>' +'<p>Data:your name is '+querystring.parse(reqBody).name+'</p>'); res.end(); }) }else{ res.writeHead(404); res.write('Not Found'); res.end(); } }).listen(3000,function () { console.log('server is listening 3000'); });
After submission, you need to obtain the request body of the request information, because the information in the POST method is in the request body. Use req to bind the data event to obtain the data. You need to pay attention here. What is important is that the data must be operated after the data reception is completed, that is, the end event must be bound to monitor whether the request information has been transmitted.
Querystring is a query string module, used to parse query strings. The parse method parses the query string into an object. Run the server in git and get the page like this:
After submitting the data:
The above is the detailed content of Example of http form submission in Nodejs. For more information, please follow other related articles on the PHP Chinese website!