Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-iroute

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-iroute

express route integrated with interceptor system

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-69.23%
Maintainers
2
Weekly downloads
 
Created
Source

express-iroute

github package.json version libraries.io dependency status for latest release node-current npm test

An express route integrated with an simple interceptor system.

Installation

  • NPM
$ npm install express-iroute
  • Yarn
$ yarn add 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(req, res, next) {
      if (req.headers['user-agent'].indexOf('mobile') === -1) {
        res.redirect('http://www.foo.com');
        return;
      }
      next();
    },
  },
  {
    flag: 'REQUIRE_LOGIN',
    path: /^\/(?!login)/,
    preHandler(req, res, next) {
      const userInfo = userService.getUserInfo(req);
      if (!userInfo) {
        res.redirect(`/login?redirecturl=${encodeURIComponent(req.url)}`);
        return;
      }
      next();
    },
  },
  {
    flag: 'REQUIRE_CSRF',
    preHandler(req, res, next) {
      // do some csrf check
    },
  },
  {
    flag: 'EXACT_REQUIRE_CORS',
    exact: true,
    preHandler(req, res, next) {
      // do some cors check
    },
  },
];

Interceptor API

exact: Boolean can be used as the trigger to turn on or off the perfect path matching pattern, the path matching rules can be found in path-to-regexp@v0.1.7 (the same version used in express).

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(req, res, next) {
    // do something...
  },
};

// list of route objects

module.exports = [
  {
    path: '/category/:id',
    method: 'GET',
    handler(req, res, next) {
      // do something...
    },
  },
  {
    path: '/category/:id',
    method: 'POST',
    interceptors: 'REQUIRE_LOGIN',
    handler(req, res, next) {
      // do something...
    },
  },
];

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

const express = require('express');
const iroute = require('express-iroute');

const interceptors = require('./interceptors');

const app = express();

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

Special Thanks

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

License

MIT

Keywords

FAQs

Package last updated on 10 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc