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!' });
});
});
can anyone support with this.if this information is not enough please inform me.
Thanks
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!' });
});
});
can anyone support with this.if this information is not enough please inform me.
Thanks
Share Improve this question asked May 29, 2017 at 18:00 Shan PeirisShan Peiris 1831 gold badge4 silver badges17 bronze badges1 Answer
Reset to default 2The 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.