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

@aspirejo/express-route-generator

v0.1.3

Published

Route generator for express app

Downloads

37

Readme

express-route-generator

Auto generate routes for express app. This library uses a defined pattern to glob the routes handlers files and dynamically add them to the express app.

Version

0.1.2

Usage

import routeGen from '@aspirejo/express-route-generator';
import express from 'express';

const app = express();
const config = {
  pattern: `${path.resolve(__dirname, '../..')}/**/controllers/**/_*.js`,
};
routeGen(app, config);

app.listen(3000, () => { console.log('listening to port 3000!'); });

Or

const routeGen = require('@aspirejo/express-route-generator');
const express = require('express');

const app = express();
const config = {
  pattern: `${path.resolve(__dirname, '../..')}/**/controllers/**/_*.js`,
};

routeGen.generate(app, config);
app.listen(3000, () => { console.log('listening to port 3000!'); });

Config

{
  "pattern": ...,
  "logger": ...,
  "routingMethodsRegex": ...,
  "globalMiddlewares": [...],
  "baseUrl": ...,
  "versioning": {
    ...
  }
}

| Property | Type | Usage | | ------------- | ------------- | ------------- | | pattern | string | files globbing pattern (required) | | logger | object | object to be used in logging | | baseUrl | string | used to construct routes, uses a formatted string | | routingMethodsRegex | regex | used to match the targeted files from the globbed list | | globalMiddlewares | array<function> | to register global middlewares for all routes | | versioning | object |used for versioning settings |

pattern

Default value : undefined

Used to glob all files that matches that pattern using glob. This property must be provided.

logger

Default value: console

By default console is used to log actions. You can use any custom logger as long as it contains a debug function.

baseUrl

Default value: /api/{0}

A string format that will be used to generate the router handlers prefixes; each found version will has its own router handler with a prefix generated using this format.

routingMethodsRegex

Default value: /_(delete|get|post|put|patch).(v\d+)(.\S+)*.js$/

Used to filter the globbed files by matching against this regular expression.

Router handlers are promisified using express-promise-router package

globalMiddlewares

Default value: undefined

Contains an array of express middlewares that will be applied on all generated routes.

versioning

Default value: undefined

Versioning settings, the expected structure as follows

{
  "versioning":{
    #VERSION_NAME# :{
      "middlewares" : [#MIDDLEWARE#],
      "paramsMiddlewares" : [
        {
          #PARAM_NAME# : #MIDDLEWARE#
        }
      ]
    }
  }
}
  • #VERSION_NAME# : version name, eg: v1, v2
  • #MIDDLEWARE# : express middleware handler(s)
  • #PARAM_NAME# : router parameter name

eg:

versioning: {
  v1: {
    middlewares: [(req, res, next) => { console.log('v1 middleware'); next(); }],
    paramsMiddlewares: [{ id: ((req, res, next) => { console.log('id parameter middleware'); next(); }) }]
  }
}
../api/v1/users/{id}/
[1]----↑         ↑
[2]--------------↑

in the above example

  • for all v1 routes v1.middlewares will be applied.
  • for all v1 routes with id path parameter v1.paramsMiddlewares[id] will be applied.

Default config

{
  logger: console,
  pattern: undefined,
  routingMethodsRegex: /_(delete|get|post|put|patch)\.(v\d+)(\.\S+)*\.js$/,
  baseUrl: '/api/{0}',
  globalMiddlewares: [],
  versioning: { }
};

Constraints

All routes handlers must:

  • Exits under a folder that matches the provided pattern
  • Name must match the routingMethodsRegex, for the default config the name must contain at the following parts seperated by dot '.'
    • http method name prefixed with _; eg: _get
    • version name; eg: v1
    • a descriptive part to distinct multiple methods within the same folder (optional)
    • extension, this should be js
  • The handler file must export:
    • route : a string representing the route that will be registered
    • controller : a function to execute
    • middlewares : an express middlewares array that will be executed for a specific route (optional)
    • aliases : a string array that represent aliases for the same route (optional)

Test

Run

npm run test

Code coverage

Run

npm run cover

Example

A sample project can be found here.