Home > Web Front-end > JS Tutorial > body text

Example of http form submission in Nodejs

零下一度
Release: 2017-07-09 10:15:19
Original
1158 people have browsed it

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([
  &#39;<form method="post" action="/url">&#39;,
  &#39;<h1>My Form</h1>&#39;,
  &#39;<fieldset>&#39;,
  &#39;<label>Personal Information</label>&#39;,
  &#39;<p>What is your name?</p>&#39;,
  &#39;<input type="text" name="name">&#39;,
  &#39;<button>submit</button>&#39;,
  &#39;</form>&#39;
  ].join(&#39;&#39;));
  res.end();
 }else if(&#39;/url&#39;==req.url&&req.method==&#39;POST&#39;){
  var reqBody=&#39;&#39;;
  req.on(&#39;data&#39;,function (data) {
  reqBody += data;
  });
  req.on(&#39;end&#39;,function () {//用于数据接收完成后再获取
  res.writeHead(200,{&#39;Content-Type&#39;:&#39;text/html&#39;});
  res.write(&#39;you have sent a &#39;+req.method+&#39; request\n&#39;);
  res.write(&#39;<p>Content-Type:&#39;+req.headers[&#39;content-type&#39;]+&#39;</p>&#39;
   +&#39;<p>Data:your name is &#39;+querystring.parse(reqBody).name+&#39;</p>&#39;);
  res.end();
  })
 }else{
  res.writeHead(404);
  res.write(&#39;Not Found&#39;);
  res.end();
 }
 }).listen(3000,function () {
 console.log(&#39;server is listening 3000&#39;);
 });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template