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

api-express-versioning

v1.0.6

Published

API versioning middleware for Express to avoid duplicating controllers

Downloads

348

Readme

Express API Versioning

A middleware for Express.js to handle API versioning without duplicating controllers.

Installation

npm install api-express-versioning

Features

  • Automatic Version Handling: Manage multiple API versions with ease based on folder structure.
  • Fallback Mechanism: If a route is not found in the requested version, the middleware will automatically check previous versions (e.g., if v4 doesn't have a route, it will try v3).
  • Version Headers: Specify the API version via headers using api-version: v1. The middleware selects the correct route based on the version provided.
  • Custom Error Messages: Return customizable error responses when a route is not found.
  • No Controller Duplication: Prevents duplicating controller logic across versions. If a specific route is not defined in a version, it reuses the logic from earlier versions automatically.

Folder Structure

Ensure your folder structure follows the pattern below. All version folders should start with the same prefix (e.g., v1, v2, etc.).

/src
  /controllers
    /api
      /v1
      /v2
      /v3
      /v4
      /v5
  /routes
    /api
      /v1
      /v2
      /v3
      /v4
      /v5
   index.js

Usage

Set up the versioning middleware in your Express app as shown below:

//FILE -  src/routes/api/index.js
const express = require("express");
const versioningMiddleware = require("api-express-versioning");

const router = express.Router();

// App router for versioned APIs
router.use(
  "/api",
  versioningMiddleware(
    __dirname + "/api", // API routes directory
    "v", // version routes base directory name - all route folders start with this prefix.
    "v1", // default version to fall back to
    // Custom error message when a route is not found
    {
      code: 0,
      message: "Route not found.",
    }
  )
);

module.exports = router;

Example Scenario

If you have 5 API versions (v1 to v5), pass the desired version via headers with api-version: v4. If the route is not found in v4, the middleware will automatically fall back to v3, continuing until it finds a route or defaults to v1.

Benefits

  • No Controller Duplication: No need to duplicate controller logic across versions. Middleware reuses logic from earlier versions if newer ones don’t have the route.
  • Simplified Version Management: Easily manage different API versions using folder structure, reducing codebase complexity.
  • Automatic Fallback: Gracefully handles requests by stepping down to previous versions if the route is unavailable in the requested version.