简体   繁体   English

Node.js http服务器

[英]Node.js http server

I am trying to create an http server that reads only POST requests and returns the body of the request in upper case. 我正在尝试创建一个只读取POST请求的http服务器,并以大写形式返回请求的主体。 This is my code: 这是我的代码:

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

When I run this from the command prompt (specifying a port number), I get the following error: 当我从命令提示符(指定端口号)运行它时,我收到以下错误:

Error connecting to http://localhost:61777: read ECONNRESET

How do I get this work? 我该如何开展这项工作?

You have to send the body, after you finish to get it. 完成之后你必须发送身体。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM