1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| var http = require('http'); var querystring = require('querystring');
var postHTML = ` <html> <head> <meta charset="utf-8"> <title>菜鸟教程 Node.js 实例</title> </head> <body> <form method="post"> 姓名: <input name="name"><br> 年纪: <input name="age"><br> <input type="submit"> </form> </body> </html>`;
http.createServer(function (req, res) { var body = ""; req.on('data', function (chunk) { console.log(chunk); body += chunk; }); req.on('end', function () { body = querystring.parse(body); console.log(body); res.writeHead(200, { 'Content-Type': 'text/html; charset=utf8' }); if (body.name && body.age) { res.write("姓名:" + body.name); res.write("<br>"); res.write("年纪:" + body.age); } else { res.write(postHTML); } res.end(); }); }).listen(3000);
|