middleware-responder

1.0.0 • Public • Published

middleware-responder

Build Status

A node.js middleware for setting render and redirect options on the req object. Specifically req.render and req.redirect.

To install:

npm install middleware-responder --save

Example usage:

routes.js

var setRedirect = require('middleware-responder').setRedirect;
var setRender  = require('middleware-responder').setRender;
 
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 '/'
};

Package Sidebar

Install

npm i middleware-responder

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • eddywashere