Home >Web Front-end >JS Tutorial >Detailed explanation of relevant knowledge of post formula in Nodejs

Detailed explanation of relevant knowledge of post formula in Nodejs

零下一度
零下一度Original
2017-05-03 10:02:511121browse

This article mainly introduces the relevant knowledge of Nodejs--post formula, which has a good reference value. Let’s follow the editor and take a look at the formula of

HTML page content

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form action="http://127.0.0.1/dopost" method="post">
  <input type="text" name="name"><br><br>
  <input type="text" name="age"><br><br>
  性别:<br>
  <input type="radio" name="sex" value="男">男
  <input type="radio" name="sex" value="女">女<br><br>
  爱好:<br>
  <input type="checkbox" name="like" value="睡觉">睡觉
  <input type="checkbox" name="like" value="画画">画画
  <input type="checkbox" name="like" value="游戏">游戏<br><br>
  <input type="submit">
</form>
</body>
</html>

NODEJS, as follows:

var http = require(&#39;http&#39;);
var querystring = require(&#39;querystring&#39;);
var server = http.createServer(function (req,res) {
  //如果你的访问地址是这个,并且请求类型是post
  if(req.url == &#39;/dopost&#39; && req.method.toLowerCase() == &#39;post&#39;){
    var allData = &#39;&#39;;
    //下面是post请求接收的一个公式
    //node为了追求极致,它是一个小段一个小段接收的。
    //接受了一小段,可能就给别人去服务了。防止一个过大的表单阻塞了整个进程
    req.addListener(&#39;data&#39;,function (chunk) {
      allData += chunk;
      console.log(&#39;数据:&#39;+allData);
    });
    //全部传输完毕
    req.addListener(&#39;end&#39;,function () {
      var oData = allData.toString();
      var iDa = querystring.parse(oData);
      res.end(&#39;success&#39;);
      console.log(iDa);
      console.log(iDa.like);
      console.log(iDa.name);
      console.log(iDa.sex);
    });
  }
})
server.listen(80,&#39;127.0.0.1&#39;);

The above is the detailed content of Detailed explanation of relevant knowledge of post formula in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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