Node.js http server

Node.js http server

本文关键字:server http js Node      更新时间:2023-09-26

我正在尝试创建一个http服务器,仅读取POST请求并以大写形式返回请求的正文。这是我的代码:

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

我如何得到这个工作?

您必须发送正文,完成后才能获取。

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]);