我正在尝试创建一个只读取 POST 请求并以大写形式返回请求正文的 http 服务器。这是我的代码:
http=require("http");
fs=require("fs");
http.createServer(function(req,res){
if(req.method=="POST")
{
var body = '';
req.on('data', function (data) {body += data.toString();});
body=body.toUpperCase()
res.end(body);
}
else
{
res.end("Not a POST request.");
}
}).listen(process.argv[2]);
当我从命令提示符(指定端口号)运行它时,出现以下错误:
Error connecting to http://localhost:61777: read ECONNRESET
我如何获得这项工作?
最佳答案
你必须在完成后发送 body 才能得到它。
http.createServer(function(req,res){
if(req.method=="POST")
{
var body = '';
req.on('data', function (data) {body += data.toString();});
// Please see this line:
req.on('end', function (data) { body=body.toUpperCase();
res.end(body);});
}
else
{
res.end("Not a POST request.");
}
}).listen(process.argv[2]);
关于javascript - Node.js HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911724/