1

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?

1

1 Answer 1

3

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]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.