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

route-middleware-mapper

v1.0.8

Published

mapping routes with middlewares

Downloads

7

Readme

router-middleware-mapper

Build Status Coverage Status

A simple package to map different routes with different middlewares in nodejs project

Getting started

Install

$ npm i -S route-middleware-mapper

or

$ yarn add route-middleware-mapper

Use

After the installation you can require the package to your nodejs project. For example,

const express = require('express');
const app = express();
const routeMiddlewareMapper = require('route-middleware-mapper')
app.use (routeMiddlewareMapper(`${__dirname}/authentication`)); 
#This path should be a valid absolute path and contains a config.json 
#and some middleware js files.

app.use(routes);
...

Policies

Create a folder named "policies" in project root. This "policies" folder contains all middlewares that are used by routes and the mapping configuration file. Other folder can be use too, if it follows the structure. Please remember DO pass the path of this folder when this library is used.(See the example above).

For example,

.
+-- policies
|   +-- config.json
|   +-- isAuthenticated.js
|   +-- isAdmin.js
|   +-- fromClient.js
|   ...

All the js files are middlewares. You can have one like this

const isAuthenticated = (req, res, next) => {
  console.dir('isAuthenticated');
};

module.exports = {
  isAuthenticated
}; # an object should be exported.

Configuration

The "config.json" file is used to map routes with middlewares. For example,

{
  "/*": ["isAuthenticated"],
  "/health": {
    "/*": [],
    "/test":["isAuthenticated"],
    "/admin": ["isAdmin"]
  },
  "/name": {
    "/userinfo": ["isAdmin"],
    "/testinfo": {
      "/:id": ["fromClient","isAdmin"]
    }
  }
}

All the keys means the routes. "/*" means all routes. "/xxx" means a specific route.
"/:" means dynamic route.

The values should be string array which contains the middlewares that required by the route. So each string here represents the middleware in "policies" folder, and the order of strings matters.

Mapping

The route will be mapped to the key that closest to it. But if the specific route is not defined in the file, more general key will be used.

In previous example,

  1. All routes need to go through "isAuthenticated".
  2. route "/hello" will go through "isAuthenticated". Because there is no closer key defined here than "/*".
  3. route "/health/info" don't have any middlewares. Beacuse its closet route is all routes("/*") under "/health", which its value is "true".
  4. route "/name/role" will go through "isAuthenticated". Beacuse although its closet route is "/name", but neither "/" nor "/role" is defined under "/name", so it turns to a more general route "/".
  5. route "/name/testinfo/2" will go through "fromClient" and "isAdmin". Because it matches the "/name/testinfo/:id".

Contribution

  1. Fork it!
  2. Create your feature branch: git checkout -b feature-branch
  3. Commit your changes: git commit -am 'Some message to describe the changes'
  4. Push to the branch: git push origin feature-branch
  5. Submit a pull request