exploring the http Object

时间:2021-05-19 04:59:51

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:\n",postData);
response.end("You Posted:\n"+util.inspect(postDataObject));
});
}
if (request.method==="GET") {
response.writeHead(200,{"Content-Type":"text/html"});
response.end(form);
}
}).listen(8080);

运行:hotnode server.js