http_post.js代码:
var process = require('process');
var listen_port = 3000;
//服务器接收请求,将相应数据写回
//客户端发出请求,接收响应
http.createServer(
function(req, res){
var buf = new Buffer(0);
//接收请求数据
req.on('data', function(data){
//data的类型是Buffer
buf = Buffer.concat([buf, data]);
});
//接收请求数据结束
req.on('end', function(){
console.log(buf.toString('Utf-8'));
res.write(buf.toString('utf-8'));
res.end();
//退出进程
process.exit();
});
}
).listen(listen_port);
console.log('Server start on port 3000');
post.html代码:
<html>
<head>
<meta charset="UTF-8" content="text/html" http-equiv="Content-Type">
</head>
<body>
<form method="POST" action="http://127.0.0.1:3000">
<table>
<thead>
<tr>
<p>这是一个POST提交到node.js服务器的例子程序</p>
</tr>
</thead>
<tbody>
<tr>
<input type="text" name="title">
</tr>
<tr>
<textarea name="content" ></textarea>
</tr>
<tr>
<input type="submit">
</tr>
</tbody>
</table>
</form>
</body>
</html>