0.1.2 • Published 7 years ago

express-iroute v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

express-iroute

Build Status npm version

An express route integrated with an simple interceptor system.

Installation

$ npm install express-iroute

Usage

Define interceptors

As this module is primarily designed for routes integrated with interceptors, the first thing is to define interceptors.

// interceptors/index.js

module.exports = [
  {
    flag: 'REQUIRE_MOBILE',
    path: '/*',
    preHandler: function (req, res, next) {
      if (req.headers['user-agent'].indexOf('mobile') === -1) {
        return res.redirect('http://www.foo.com');
      }
      next();
    }
  },
  {
    flag: 'REQUIRE_LOGIN',
    path: /^\/(?!login)/,
    preHandler: function (req, res, next) {
      var userInfo = util.getUserInfo(req);
      if (!userInfo) {
        return res.redirect('/login?redirecturl=' + encodeURIComponent(req.url));
      }
      next();
    }
  },
  {
    flag: 'REQUIRE_CSRF',
    preHandler: function (req, res, next) {
      // do some csrf check
    }
  }
];

Interceptor API

flag: String is the identity of interceptor, used for specific route to add or ignore interceptors.

path: String|RegExp|Array defines paths which this interceptor should apply, following express route path format. If not defined, no routes will apply except some specific route configs.

preHandler: Function defines interceptor function, which executes before actual route handler.

Define routes

This module heavily borrows idea and code from express-autoroute, so routes definition is exactly the same with it. Only one thing is different: you can config interceptors or ignoreInterceptors to overwrite interceptor-level configuration.

// routes/login/login.js

module.exports = [
  {
    path: '/',
    interceptors: 'APPEND_CSRF',
    handler: loginGetController,
  },
  {
    path: '/',
    method: 'POST',
    interceptors: 'REQUIRE_CSRF',
    handler: loginPostController,
  },
  {
    path: '/randomcode',
    ignoreInterceptors: 'REQUIRE_LOGIN',
    handler: randomcodePngController,
  }
];

Route API

Every route module can export an route object or a list of route object.

// single route object
module.exports = {
  path: '/category',
  method: 'GET',
  handler: function (req, res, next) { ... }
};

// list of route objects
module.exports = [
  {
    path: '/category/:id',
    method: 'GET',
    handler: function (req, res, next) { ... }
  },
  {
    path: '/category/:id',
    method: 'POST',
    interceptors: 'REQUIRE_LOGIN',
    handler: function (req, res, next) { ... }
  },
];

path: String route path, will be prefixed with directory path.

method: String|Array http verb, default is 'GET'.

interceptors: String|Array overwrite common config, add an interceptor or some interceptors.

ignoreInterceptors: String|Array overwrite common config, remove an interceptor or some interceptors.

handler: Function|Array route handlers.

Route API is just a simple wrapper of express route methods, please refer to its document for details.

Make it works

var express = require('express'),
  iroute = require('express-iroute'),
  interceptors = require('./interceptors');

var app = express();
iroute(app, {
  interceptors: interceptors,
  routesDir: './routes'
});

Special Thanks

Thanks for good ideas from express-autoroute and HandlerInterceptor from SpringMVC.

License

MIT