node.js - Update headers in a nodejs http.request -
there way in nodejs modify response headers when calling second nested http.request return data server ? error, below functions...
catch error: error: can't set headers after sent.
main function call check if token valid: req.on('end', function (){
console.log('get /api, have privileges %s?', access_token); validadtoken(req, res, next); });
this funciton validate token against oauth2 server, if respond 401, tried obtain new refresh_token:
function validadtoken (req, res, next ) { var options = { port: 8080, path: '/api', host: 'localhost', method: 'get' , headers: {'authorization': 'bearer '+ access_token, 'content-type':'application/x-www-form-urlencoded'} }; req2 = http.request(options, function (response){ response.setencoding('utf8'); response.on('data', function (chunk) { console.log(".-response=> ", chunk); console.log("statuscode: " + response.statuscode); if(response.statuscode == 401){ console.log(); console.log("===>>>> call renew / wait me"); renewtoken(response, res, next); }else if(response.statuscode === 200){ console.log("valid token... keep going!"); } next(); }); }); req2.write(formdata); req2.end(); }
from here try refresh token, when try update new value in headers above error. value refresh_token recovery response function
function renewtoken (req, res){ var userurl = 'grant_type=refresh_token&refresh_token='+refresh_token+'&client_id=xxx&client_secret=xxx'; var optrenew = { port: 8080, path: '/token', host: 'localhost', method: 'post' , headers: {'content-type':'application/x-www-form-urlencoded'} }; renew = http.request(optrenew, function (responserenew){ console.log("responserenew"); responserenew.setencoding('utf8'); responserenew.on('data', function (chunk){ console.log(".-responserenew=> ", chunk); console.log(".-responserenew=> ", responserenew.statuscode); accesstokenvalue = gettoken("access_token", chunk, ',', ':', '"'); refreshtokenvalue = gettoken("refresh_token", chunk, ',', ':', '"'); console.log("headers sent (renew) ? "+ res.headerssent); console.log("¡¡¡new values!!!\n" + accesstokenvalue +' '+ refreshtokenvalue); res.setheader("set-cookie", ["ninja="+accesstokenvalue, "samurai="+refreshtokenvalue]); console.log("envio ...."); }) }); renew.write(userurl); renew.end(); }
i think found
if(response.statuscode == 401) { console.log(); console.log("===>>>> call renew / wait me"); renewtoken(response, res, next); } else if(response.statuscode === 200) { console.log("valid token... keep going!"); } next();
when token doesn't need renewed call next
, ok, when renewtoken
called have problem: http.request
asynchronous call, renewtoken
returns before code process renew response executed, call next
done before renewed token known. when call next
next middleware called, here next middleware send response before renewed token available , explains error: after next middleware completes response try set 1 of response headers
the solution easy (i'll skip code)
function validadtoken(req, res, next) { ... req2 = http.request(options, function (response) { response.setencoding('utf8'); response.on('data', function(chunk) { ... if (response.statuscode == 401) { ... renewtoken(req, res, next); } else if (response.statuscode === 200) { console.log("valid token... keep going!"); next(); } else { // todo: should deal other cases } }); }); req2.write(formdata); req2.end(); } function renewtoken(req, res, next) { ... renew = http.request(optrenew, function (responserenew){ ... responserenew.on('data', function(chunk){ ... res.setheader("set-cookie", ["ninja="+accesstokenvalue, "samurai="+refreshtokenvalue]); console.log("envio ...."); // it's time call next middleware next() }) }); renew.write(userurl); renew.end(); }
tl;dr; should call next
after got token
Comments
Post a Comment