Simha Chalam Simha Chalam - 10 days ago 6
AngularJS Question

when i set the token in header in angularjs generating error in nodejs server?

Error is:

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


1.all nodejs services working fine before set the token in header.

2.after set the token in header in angularjs creating that error in nodejs server.

my angularjs code is :

menu.factory('allServs', function ($q, $http,$cookieStore) {
propDetails:function (property_id) {

var req = {
method: 'GET',
url: '/v1/xxxxxxx/'+xxxxxxx
}

if($cookieStore.get('userDetails')){
req.headers = {
"authorization": $cookieStore.get('userDetails').token
}
}
var deferred = $q.defer(),
httpPromise = $http(req);
httpPromise.then(function (response) {
alert("response"+JSON.stringify(response));
deferred.resolve(response);
}, function (error) {
alert("error"+JSON.stringify(error));
console.error(error);
});
return deferred.promise;
}
}


3.In nodejs ,webservice token is getting ,decoding is fine and getting the response details but the problem is last line at the time of return json data.

app.get('/v1/xxxxxxx/:xxxxx',middlewares.requireLogin,function(req, res) {

core.property.getProjectGroup(options, function (result) {

return res.send({
result:result
});
});

});



  1. middlewares.requireLogin(token verify process) is working fine.

  2. I solved that error by using this line res.status(200).send().In that process in client its showing success code is 400.

  3. so how can i solve that one. i want to get the data and status code as 200


Answer

You are calling res.send() or res.end() twice somewhere in your code. Kindly check with the middleware, or other places with the response object if it calls a function that sends out an HTTP response BEFORE your last res.send() that you intended.