Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do do a callback after redirect in expressjs

I am redirecting to a page using

res.redirect('/pageOne');

so after the redirect i want call a function , But when i called the function

 res.redirect('/pageOne');
 callBack();

I found that the callBack executed before the redirect,

so there any way to pass callBack to redirect ? according to the api its no. any other workaround ?

like image 572
Sarath Avatar asked Jul 14 '13 07:07

Sarath


1 Answers

According to the current implementation, there is no way to pass a callback into the redirect method. However, since the response object res inherits from Node's own ServerResponse, which in turn is a Writable stream, you can watch for the related events. For example, you might try the finish event:

Event: 'finish'

When end() is called and there are no more chunks to write, this event is emitted.

So, for example,

res.on('finish', callback);
res.redirect('/pageOne');
like image 169
Michelle Tilley Avatar answered Sep 28 '22 01:09

Michelle Tilley