I am trying to create an http server that reads only POST requests and returns the body of the request in upper case. 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]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.