Reputation: 1269
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?
Upvotes: 1
Views: 136
Reputation: 24630
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]);
Upvotes: 3