Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
render & redirect helper middleware

Example usage:

This middleware uses the request object to set arbitrary data available at either req.render or req.redirect;

routes.js

var setRedirect = require('respond-middleware').redirect;
var setRender  = require('respond-middleware').render;

app.get('/', 
  setRender('home'), // req.render = 'homepage';
  userController.home);

app.post('/login', 
  setRedirect({failure: '/', success: '/dashboard'}), // req.redirect = {failure: '/', success: '/dashboard'};
  userController.postLogin);

user-controller.js

exports.home = function(req, res, next){
  // app logic here

  res.render(req.render); // 'home'
};

exports.postLogin = function(req, res, next){
  // login logic here
  if(req.user){
    return res.redirect(req.redirect.success); // redirects to 'dashboard';
  }

  res.redirect(req.redirect.failure); // redirect to '/'
};
@eddywashere
Copy link
Author

eddywashere commented Oct 12, 2014

should add warnings when the middleware is called without any values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment