form.html
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="" method="post"> <input type="text" name="userinput1"> <input type="text" name="userinput2"> <input type="submit"> </form> </body> </html>
server.js
var http=require("http"); var querystring=require("querystring"); var util=require("util"); var form=require("fs").readFileSync("form.html"); http.createServer(function(request,response){ if (request.method==="POST") { var postData=""; request.on('data',function(chunk){ postData+=chunk; }).on("end",function(){ var postDataObject=querystring.parse(postData); console.log("User Posted: ",postData); response.end("You Posted: "+util.inspect(postDataObject)); }); } if (request.method==="GET") { response.writeHead(200,{"Content-Type":"text/html"}); response.end(form); } }).listen(8080);
运行:hotnode server.js