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-path-router

v1.0.0

Published

A router for Express.js based on the filesystem

Downloads

19

Readme

express-path-router

travis build Coverage Status NPM Version

Description

This module allows to mount an express API based on a file architecture and facilitate the middleware reuse.

Installation

yarn add express-path-router

Usage

Following the classic express routing way, a huge service may end with a really fastidious bunch of code (even when splitted in multiple files). This module allows to avoid this by loading the routing from file architecture.

Example:

API target:

app.get('cities/:cityId', [... middlewares ...], callback);
app.post('companies', [... middlewares ...], callback);
app.get('companies/:companyId', [... middlewares ...], callback);

File system:

|-- index.js
|-- api
        |-- cities
        |        |-- :cityId
        |                   |-- get.js
        |-- companies
                    |-- post.js
                    |-- :companyId
                                |-- get.js

Then, you can load it automatically

var router = require('express-path-router');

router.load({
    app: app,
    path: __dirname + '/api'
  });

Documentation

Each API point (file like .../api/companies/:companyId/get.js) is based on the following structure:

module.exports = {
    pattern: string,
    middlewares: [function, ..., function],
    callback: function
}
  • pattern - string - optional - pattern replacement (ie: '(*)' will replace a get:/products/:id by get:/products/(*))
  • middlewares - function[] - optional - list of middlewares to add on the route
  • callback - function - API point payload

A syntactic sugar may be used in the module, by returning a function or an array of function. Thoses functions will be set as middleware and callback.

_.js

Specials files _.js may be added everywhere in the file structure. Thoses files middlewares will be added on each API point in their sub-structure.

Example:

|-- companies
            |-- _.js    {middlewares: [A, B, C]}
            |-- post.js {middlewares: [D], callback: E}
            |-- :companyId
                        |-- _.js {middlewares: [F, G]}
                        |-- get.js {middlewares: [H, I, J], callback: K}

This example will produce the equivalent of:

app.post('companies', A, B, C, D, E);
app.get('companies/:companyId', A, B, C, F, G, H, I, J, K);

load(config, callback)

  • config - object

    • app - object - express application
    • path - string - path to load
    • prefix - string - optional - prefix to add on the route
    • hook - function - optional - function call before each route definition to modify the middleware stack
  • callback - function - optional

returns undefined when using callback, else a promise.

hook(middlewares, data)

  • middlewares - function[]
  • data - object
    • file - string - working file (ie. .../api/companies/:companyId/get.js)
    • method - string - http verb (ie. get)
    • module - object - module loaded (ie. get.js module)
    • modules - object[] - ordered list of module (ie. _.js, ...., get.js module list)
    • route - string - target route (ie. companies/:companyId)

The hook function is useful to modify the middleware stack before adding the route. You can either modify the middlewares parameters or return a new array.

Example:

var auth = require('../middlewares/auth');
var token = require('../middlewares/adminToken');

router.load({
    app: app,
    prefix: 'admin',
    path: __dirname,
    hook: function (middlewares, data) {
      logger.info('    ADMIN: ' + data.route + ' [' + data.method + ']');
      middlewares.unshift(token);
      if (data.module.registered) {
        middlewares.unshift(auth);
      }
    }
  });

License

MIT