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 🙏

© 2025 – Pkg Stats / Ryan Hefner

endpoint-routing

v2.1.2

Published

A directory-based HTTP request router.

Readme

A directory-based HTTP request router.

Version

The endpoint-routing package contains functionality to translate your project directory into the endpoints of your express web server. With support for URL variables, conditional path imports, and mock HTTP calls.

You can install this package via npm:

npm install endpoint-routing

Table of Contents

Usage

With a dedicated endpoints directory...

endpoints/
├── get.html (The Homepage)
├── dashboard/
│   ├── get.js
│   └── settings/
│       ├── get.js
│       └── post.js
├── users/
│   └── [userId]/
│       └── get.js
├── login/
│   └── get.html
└── register/
    └── get.html

...endpoints become immediately accessable. No middleware needed.

import express from 'express'
import initializeRouting from 'endpoint-routing'

const app = express()
initializeRouting(app)

app.getWithRouting()

app.listen(3000)

All respective request methods have equivalent *WithRouting versions, which handle pointing to the desired endpoint, such as:

  • getWithRouting()
  • postWithRouting()
  • putWithRouting()
  • deleteWithRouting()
  • patchWithRouting()
  • useWithRouting()

Structuring your endpoint files

The first step is to reserve a folder specifically for the endpoints of your server, with the name of the files in each directory being the HTTP method used to access it's contents, and the file extension being how it's interpreted. An example endpoints directory would look like:

When we're finished setting up your project, the directory path ./endpoints/dashboard/settings/get.js is translated to domain.com/dashboard/settings for your web server.

The file names can be any of the primary HTTP methods, and the supported file extensions are as follows:

  • .js / .mjs / .cjs JavaScript Files - If a callable function is the default export of the file, it will be invoked with (req, res) passed in as parameters, with the third parameter being used for the next callback if the callback takes three parameters
  • .html HTML - Render an HTML file (This requires you to define an engine with Express)

Pre-compiling routes

With the current implementation, without any pre-computation of our routes, incoming requests directly traverse the endpoints folder to see if a route is valid. Of which, you don't need me to tell you that is hardly efficient.

A call to buildEndpointRoutes will construct a registry of all the exposed endpoints incoming requests can utilize.

import { buildEndpointRoutes } from 'endpoint-routing'

const args = {
    // The relative path to the file where the compiled routes should be written to
    output: 'routes.json',
    // The parent folder we're compiling these endpoints from
    endpoints: 'endpoints',
    // Endpoint paths to exclude
    pathBlacklist: ['dev'],
    // Log status updates
    debug: true,
}
await buildEndpointRoutes(args)

In this example, this is a lone file that can be manually ran with the node command. Or if you're using Docker, you can hook it to run on scripts.deploy inside of your project's package.json to generate on deployment.

[!NOTE] This points to the endpoint files instead of storing a copy of the functions, so updates to the callback don't require a re-compile of the routes.

Now, you're all set up to preform a(n efficient) request!

import express from 'express'
import initializeRouting from 'endpoint-routing'

const app = express()
initializeRouting(app, 'routes.json') // Linked pre-compiled routes

app.getWithRouting()

app.listen(3000)

Additional features

URL variables

When defining the endpoint paths in your project files, you can wrap pathnames with square brackets to indicate variables, similar to that of /:variable in traditional middleware.

endpoints/
└── users/
    └── [userId]/
        └── index.js

The value within the URL will be injected into the Express-provided request object under the params key. For example, a request to this:

/users/4124

Will translate into this under req.params:

{ userId: "4124" }

Endpoint existance checks

Optionally, you can check that the endpoint at the requested path and method exists before continuing in your middleware.

app.use(async (req, res, next) => {
    const doesEndpointExist = await app.doesEndpointExist(req.path, req.method)
    if (!doesEndpointExist) {
        req.status(404).json({error: "Route not found."})
        return
    }
    next()
})