0

Here I am trying to access redirect on login route by same route, first i am calling homeCtrl function ,after this function successfully render i want to exceute res.redirect('/login'),

but here follow error is occured-:

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

router.get('/email_authentication', homeCtrl,function(req,res){

res.redirect('/login');
});

function homeCtrl(req, res,next) {

res.render('email_authentication');

return next();

}

please guide me how to do this if any other solution please help me.

  • possible duplicate of https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client – Arif Khan Jun 05 '18 at 06:02
  • res.render ends your middleware chain and you cannot process next steps. – scetiner Jun 05 '18 at 06:20
  • @scetiner if res,render is not possible so how can i get my result in the same case. – coin-o-pedia c Jun 05 '18 at 06:32
  • I dont get the logic you want to implement. this implementation tries to render email_auth and login at the same time. – scetiner Jun 05 '18 at 06:41
  • @scetiner actually at the first time while route has call email_auth page should be view , but after some verification process i want to redirect on login route.so that i am going with this scenario. – coin-o-pedia c Jun 05 '18 at 06:44
  • they are completely seperate things, first render email verification page with "get", then use form post to send your parameters and render login if needed – scetiner Jun 05 '18 at 07:05
  • @scetiner ok dude ....thanks – coin-o-pedia c Jun 05 '18 at 07:26

1 Answers1

0

request response cycle ends when you send a response.

when user send a request to /email_authentication homeCtrl will does his job and render email_authentication template. the cycle ends there.

I don't get your logic. when you render a template you should wait for next request from user. Nut if you really need to redirect user to another page (i don't see the point) you can use front end redirect in the email_authentication template.

window.location.replace('/login');

better explanation will helps us to help you.

defectivepixel
  • 575
  • 1
  • 4
  • 22