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