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-route-versioning-middleware

v2.0.0

Published

Middleware for versioning functions for express routers. Use 'accept-version' header on request. Does not dictate express.Request, express.Response, or express.NextFunction types

Downloads

12

Readme

Express Route Versioning Middleware

ExpressRouteVersioning Test

No more versioning routes by adding parameters to the url or adding to the path name. Simply add accept-version: versionNumber to the request headers and let your middleware choose the correct function.

Why use this version

This version does not dictate your express.Request, express.Response, or NextFunction types for the RouteHandler. So if you have a middleware before/after that adjusts response.locals to a strict type this will play nicely in TS.

Supports Express 5!

Usage

Version numbers must be in ascending order

Basic express usage

import express, { Request, Response, NextFunction } from 'express';
import { routeVersionHandler } from 'express-route-versioning-middleware';

const app = express();
const router = express.Router();

app.use(router);

//curl --header "accept-version: 0.1.5" localhost:8081/doThings
//Will hit testFunc2
router.get('/doThings', routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.2.3': testFunc3
}));

Same example but with middleware for auth before route versioning.

//curl --header "accept-version: 0.1.5" localhost:8081/doThings
//Will hit testFunc2 with fancyAuthMiddleware's next()
router.get('/doThings', fancyAuthMiddleware, routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.2.3': testFunc3
}));

Versioning Logic

Run the highest version function where requester's version is >= function version

In simple terms

'minimumVersionRequired': functionToCall

Examples:

routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.0.0': testFunc3,
    '1.2.3': testFunc4
})

testFunc1
curl --header "accept-version: 0.0.0" localhost:8081/doThings

testFunc1
curl --header "accept-version: 0.0.999" localhost:8081/doThings

testFunc3
curl --header "accept-version: 1.2.2" localhost:8081/doThings

testFunc4
curl --header "accept-version: 1.2.3" localhost:8081/doThings

Version Information

  • 1.x.x Supports Express ^4.x.x
  • 2.x.x Supports Express ^5.x.x