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

auto-route-loader

v1.2.0

Published

Automatically traverse an Express project and load routes.

Downloads

11

Readme

route-loader

Automatically traverse an Express project and load routes.

route-loader will recursively traverse the project directory structure loading routes.js files from whitelisted directories.

Given some project with the following directory structure:

project/
  auth/routes.js
  account/routes.js
  feature1/routes.js
  feature2/routes.js
  ...

And given some example router file:

// project/auth/routes.js

module.exports = function (router, opts = {}) {
  const db = opts.db;

  router.post(
    '/authenticate',
    (req, res) => { ... }
  );

  router.get(
    '/logout',
    (req, res) => { ... }
  );
}

The following is an example of how to load all the routes in that project.

WARNING: If you decide to put your route configuration in a separate file in your project root, be certain that the filename you choose does not match your route file regular expression (e.g. routes.js by default). This would cause an infinte require loop.

// project/app.js

const express = require('express'),
  pgp = require('pg-promise')(),
  routeLoader = require('route-loader');

const routerFactoryFn = () =>
  express.Router({
    strict: true,
    mergeParams: true,
    caseSensitive: false
  });

const app = express();
const db = pgp(...);
const rootRouter = routerFactoryFn();
const loader = routeLoader(routerFactoryFn, {
  directoryWhiteList: [
    'auth', 'account', 'feature1', 'feature2'
  ],
  routerOptions: {db: db}
});

loader.loadRoutes(__dirname, rootRouter);

app.use('/api', rootRouter);

Routes will be created at the following paths:

/api
  auth/
    authenticate (POST)
    logout (GET)
  account
  feature1
  feature2

Route Loader

Route Loader is configured with several options and returns a loader object with one function loadRoutes().

routeLoader(routerFactoryFn, options)

  • routerFactoryFn: required a router factory function (see Router Factory below)
  • options: optional an options object
    • directoryWhiteList: a list of directory basenames from which loading route files is permissible, empty by default
    • re: a regular expression against which to match route filenames, default is /routes\.js/
    • routerOptions: an arbitrary object to be passed on to the route files as they're loaded - e.g. a database connection object

loadRoutes()

loader.loadRoutes(<root path>, <root router>)

  • root path: the path from which to recursively descend and load route files. If loading routes from app.js in the project's root directory, use __dirname
  • root router: the router to which all descendant routers attach, typically created with the previously defined routerFactoryFn

Router Factory

A router factory function is necessary to create new routers for each routes.js file using the same configuration. The factory function is passed into a routeLoader.

const express = require('express')
  routeLoader = require('route-loader');

const routerFactoryFn = () =>
  express.Router({
    strict: true,
    mergeParams: true,
    caseSensitive: false
  });

const loader = routeLoader(routerFactoryFn);