1

when i run my web application i get this error.this was happened suddenly.my application is MEAN stack appllication whicj has a end to end connection through APIs.

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.res.set.res.header (D:

this is an API that is being exposed.

app.post('/api/filesReadUpload', function(req, res) {

    var eventReadUpload=new filesReadUpload({
      //id:req.body.id,
        dat:req.body.dat,
        details:req.body.details,
        tim:req.body.tim,
      //  space:req.body.space

    });

    eventReadUpload.save(function(err) {
      if (err)
          res.send(err);

      res.json({ message: 'Read file!' });
});
        });

enter image description here

can anyone support with this.if this information is not enough please inform me.

Thanks

1 Answer 1

2

The ERR_CONNECTION_REFUSED appears because the server is down. The reason why the server is down is because of the error you pointed:

Error: Can't set headers after they are sent.

That means you tried to send the response twice. In case of error you have to run return res.send(err) because you want to not call the res.json in case of error. If you do (what currently is happening), you will get that error.

eventReadUpload.save(function(err) {
  if (err)
    return res.send(err);

  res.json({
    message: 'Read file!'
  });
});

You will have to figure out why the saving fails, but this will fix the initial problem.

Sign up to request clarification or add additional context in comments.

2 Comments

error solved.Thank you very much.As u told have to find why the saving is not working.if you have any suggestion please let me know..
@Shan Ask a new question for that and mention what the error is.

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.