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

expressjs-route-mapper

v0.0.11

Published

HTTP dynamic routing with Express.js

Downloads

40

Readme

expressjs-route-mapper

HTTP dynamic routing with Express.js

About

The motivation for this module is to provide a high-level abstraction of routes in express.js. It is written in JavaScript, and is 100% MIT licensed.

  • a simple, easy method for generating restful routes for a model.
  • easy declaration of your "root" or "home" route.
  • support virtualhost, domain specific routes
  • support condition inheritance
  • optional definition of a "before wrapper" function, which is called before request handling is made.
  • optional support middleware

Install

npm install expressjs-route-mapper --save

Supported HTTP methods:

  Method    Scope         Semantics
  GET       collection    Retrieve all resources in a collection
  GET       resource      Retrieve a single resource
  HEAD      collection    Retrieve all resources in a collection (header only)
  HEAD      resource      Retrieve a single resource (header only)
  POST      collection    Create a new resource in a collection
  PUT       resource      Update a resource
  PATCH     resource      Update a resource
  DELETE    resource      Delete a resource
  COPY      resource      Clone/Copy of an existing resource.
  PURGE     resource      pick out an object from the cache and discard
  TRACE     resource      TRACE allows the client to see testing or diagnostic information.
  OPTIONS   any Return available HTTP methods and other options

Examples

Here is an example on how to use it:

Basic route

  express = require("express");
  app = express();
  require('expressjs-route-mapper').use(app);

  app.routeMapper({
    base: '/users/:user_id/tables/:table_id',
    routes: {
      '/stories': {
        'get': function(req, res){ res.send('hello get'); },
        'delete': function(req, res){ res.send('hello delete'); },
        'put': function(req, res){ res.send('hello put'); },
        'post': function(req, res){ res.send('hello post'); },
      }
    }
  });

These are the routes that will get created with the code above:

Method    URL
GET       /users/:user_id/tables/:table_id/stories
POST      /users/:user_id/tables/:table_id/stories
DELETE    /users/:user_id/tables/:table_id/stories
PUT       /users/:user_id/tables/:table_id/stories

Basic route with before wrapper

Here we have route [IndexRoute] with before callback [IndexRoute.before] which run before the main route IndexRoute. This is useful for security check or validate a user before render the main route

  • note: before condition inherits down to child routes
  express = require("express");
  app = express();
  require('expressjs-route-mapper').use(app);

  IndexRoute = function(req, res){
    return 'Hello World'
  }

  IndexRoute.before  = function(req, res){
    return 'Hello World'
  }

  app.routeMapper({
    base: '/home',
    routes:
      'get': function(req, res, next){
        res.send('Hello World');
      }
  });

Basic route with base root

Declare your root or home url with the base options:

  app.routeMapper({
    base: '/product',
    routes: {
      'get': function(req, res, next){
        res.send('Hello World');
      },
      '/category':{
        'get': function(req, res, next){
          res.send('Hello World');
        }
      }
    }
  });

These are the routes that will get created with that method call:

Method  URL
GET     /product
GET     /product/category

Route mapping with multiple layers

  express = require("express");
  app = express();
  require('expressjs-route-mapper').use(app);

  app.routeMapper({
    routes: {
      '/product': {
        'get': function(req, res){
          return "Hello World, I'm an http get method"
        },
        'post': function(req, res){
          return "Hello World, I'm an http post method"
        },
        '/byId':{
          'get': function(req, res){
            return "my product id is 10"
          },
          'delete': function(req, res){
            return "Hello World, I'm an http delete method"
          },
        }
      }
    }
  });

These are the routes that will get created with that method call:

Method    URL
GET       /product
POST      /product
GET       /product/byId
Delete    /product/byId

Virtual Host

domain specific routes

  express = require("express");
  app = express();
  require('expressjs-route-mapper').use(app);
  myDomainServer = express();
  myDomainServer.get('/', function(req, res){
    res.send('hello one.lvh.me');
  })

  app.routeMapper({
    host: 'one.lvh.me',
    server: myDomainServer, //optional server
    base: '/my-route',
    routes: {
      'get': function(req, res){ res.send('hello one.lvh.me/my-route'); },
    }
  });

  app.routeMapper({
    host: 'two.lvh.me',
    base: '/your-route',
    routes: {
      'get': function(req, res){ res.send('hello two.lvh.me/your-route'); },
    }
  });

These are the routes that will get created with that method call:

Method    URL          Domain
GET       /my-route    http://one.lvh.me/my
GET       /your-route  http://your-domain/your-domain

Supported domain patterns:

  lvh.me        //domain www
  www.lvh.me    //sub-domain www
  *.lvh.me      //any sub-domain

Todo

  • ways to customize, override and extend the generated restful routes

Notes

Inspired by express.js examples

License

MIT © Carlos Marte