res.redirect('back') with parameters

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, showing some error message. The problem is that I do not know from which page the user performed the registration, so I use res.redirect('back');. However, I cannot just redirect user back, I have to display the error message also, so I have to pass some argument. But res.redirect('back', (reg_error:'username')}) cannot be used directly because res.redirect() does not support parameters. How can I render the previous page with some parameter?


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

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 .

The most important difference is using an explicit 'bounce' parameter in the query string that overrides the Referer url.

A really easy way of implementing this is to use connect-flash, a middleware for express that leverages the session middleware built into express to pass 'flash' messages to the next request.

It means you don't need to add fields to track urls or messages to the form or route patterns for your views.

It provides a simple version of the same feature in Ruby on Rails.

(obviously you have to be using the Express framework for this to use, but I highly recommend that too!)

Plug it in like this:

var flash = require('connect-flash');
var app = express();

app.configure(function() {
  app.use(express.cookieParser('keyboard cat'));
  app.use(express.session({ cookie: { maxAge: 60000 }}));

And then use it like this:

app.get('/flash', function(req, res){
  req.flash('info', 'Flashed message')

app.get('/', function(req, res){
  res.render('index', { message: req.flash('info') });

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 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)

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.

Need Your Help

Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

python pandas

I'm reading some automated weather data from the web. The observations occur every 5 minutes and are compiled into monthly files for each weather station. Once I'm done parsing a file, the DataFrame

How is it possible for an iframe to access its parents DOM?

javascript html iframe cross-domain

How is it possible for a script within an <iframe> to have any notion of the page containing it? Can a script in the frame access any context outside of it? What about cross-domain?