npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-iroute

v1.0.0

Published

express route integrated with interceptor system

Downloads

53

Readme

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 [email protected] (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