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

v0.3.4

Published

The laziest way to route your express app.

Downloads

7

Readme

express-router-config

The laziest way to route your express app.

Build Status NPM Version Downloads Stats

This is an effort to maximize laziness in routing express app. This tool includes grouping of routes, sorting of routes, and applying multiple middleware in most convenient way, of course the structure is inspired by react-router-config.

Features:

  • Sorting - will sort your routes, wildcards has been taken into account too.
  • Grouping - will add a prefix to your routes depending on the group.
  • Multiple Middleware - will let you add one or more middleware in most convenient way.
  • Pre/Post Middleware - let's you define whether the middleware should execute before or after executing your function

Installation

npm install -S express-router-config

Usage

import express from 'express'
import router from 'express-router-config'
import { getUserById, getUser, login } from './controllers/users'
import { restrictedRoute } from './middlewares'

/** create express server */
const server = express.createServer()
/** configure your routes */
// NOTE: you may or may not use group, but for the sake of grouping up
// your endpoints it's best to use it.
router(server)([
  {
    group: 'users',
    routes: [
      {
        match: '/:id',
        middleware: restrictedRoute,
        method: 'get',
        action: getUserById
      },
      {
        match: '/',
        method: 'get',
        action: getUser
      }
    ]
  },
  {
    match: '/login',
    method: 'post',
    action: login
  }
])

server.listen(8080)

Setting middleware's execution

You may choose to add middleware after the function's execution, in order to do that you must use the following syntax:

import * as express from 'express'
import router from 'express-router-config'
import { getUserById } from './controllers/users'
import { authenticate, logger } from './middlewares'

/** create express server */
const server = express.createServer()
/** configure your routes */
// NOTE: you may or may not use group, but for the sake of grouping up
// your endpoints it's best to use it.
router(server)([
  {
    match: '/users/:id',
    method: 'get',
    middleware: [
      ['before', authenticate],
      ['after', logger]
    ]
    action: getUserById
  }
])

server.listen(8080)

in the example above, the /users/:id route is authenticated first, then executes getUserById, after the execution of the getUserById the logger is executed right away.

Why use express-router-config?

You don't have to, but if you're used to react-router-config, then this would make it easier for you to configure your routes as it is almost similar structure to react-router-config, another advantage of using this route tool is you can easily chain your middleware. For instance if you want to protect all of your routes, and at same time a single route would require additional middleware, you'd do it like this:

import { anotherMiddleware } from './middlewares'

router(server)([
  {
    // assuming that you want to protect all routes under /users
    group: 'users',
    middleware: restrictedRoute,
    routes: [
      {
        match: '/:id',
        middleware: anotherMiddleware,
        method: 'get',
        action: getUserById
      },
      {
        match: '/',
        method: 'get',
        action: getUser
      }
    ]
  }
])

the code from above would use restrictedRoute middleware first, and if you're going to access /users/:id, it would also use anotherMiddleware middleware.

Disclaimer

  • ~~Didn't have time to write tests, don't expect that things should work the way it should.~~
  • Current releases may not be stable until further notice. I didn't have much time to tinker around. Any kind of help is appreciated.

Credits

credits to @yakovmeister for the original idea