I have a register form on every page of my website. During registration some error may occur. After catching the error, I have to return the user to the previous page, sho
If you're using sessions, you can just add that reg_error
object to the req.session
object before your redirect. Then it will be available on the req.session
object when you're loading the previous page.
you can use connect-flash package to send a error or successfull message.
https://www.npmjs.com/package/connect-flash
You should add page parameter to the form. Then you can read it on the server and use it for the redirect.
res.redirect(page, {reg_error:'username'}) // page being the url to redirect to.
Using the referer header to find what page your user came from might be helpful:
app.get('/mobileon', function(req, res){
backURL=req.header('Referer') || '/';
// do your thang
res.redirect(backURL);
});
You might also want to store backURL
in req.session, if you need it to persist across multiple routes. Remember to test for the existence of that variable, something like: res.redirect(req.session.backURL || '/')
edit: Since my answer has been getting some upvotes I typed up my current way to do this. It got a little long so you can view it at https://gist.github.com/therealplato/7997908 .
The most important difference is using an explicit 'bounce' parameter in the query string that overrides the Referer url.
You could simply have it redirect as res.redirect('..?error=1')
the ?
tag tells the browser that it is a set of optional parameters and the ..
is just a pathname relative recall (like calling cd ..
on terminal to move back one directory)
and your browser will direct to the appropriate page with that tag at the end: http://.....?error=1
then you can simply pull the error on the appropriate page by doing a:
if (req.param("error" == 1)) {
// do stuff bassed off that error match
};
you can hardcode in several different error values and have it respond appropriately depending on what error occurred
Note that this makes your webpage susceptible to manipulation since users can type in their own parameters and get your page to respond to it, so make sure the action you take is not something you wouldn't want people abusing. (Should be fine for simple error displaying)
Use a middleware before before handling requests and put a success and error variable with res object.
take reference below:-
// set local variables middleware
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
// set success flash message
res.locals.success = req.session.success || '';
delete req.session.success;
// set error flash message
res.locals.error = req.session.error || '';
delete req.session.error;
// continue on to next function in middleware chain
next();
});
on encountering the error, use another middleware to handle error and add error with req object.
app.use(function(err, req, res, next) {
console.log(err);
req.session.error = err.message;
res.redirect('back');
});
this will redirect to the previous page along with a error message in res object