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-diroutes

v1.0.0

Published

A simple package to generate an Express middleware router given a directory structure.

Downloads

12

Readme

[WIP] express-diroutes

Build Status Maintainability Test Coverage

What's This?

A simple package to generate an Express middleware router given a directory structure.

Usage

To get started, install this via npm:

npm i express-diroutes

You can then use it in your application with:

const path = require('path');
const expressDiroutes = require('express-diroutes');

expressDiroutes({
  rootPath: path.join(__dirname, './path/to/routes'),
});

The Routes Directory

The routes directory contains files named after their endpoints. The following is an example of how to structure a directory and their corresponding routings:

- /{index}.js
- /{star}.js
- /session.js
- /user/create.js
- /user/read.js
- /user/update.js
- /user/delete.js
- /users
- /users/{_}user_id.js
- /profiles/{_}profile_id.js

The above directory structure will result in registration of routes:

/session
/user/create
/user/read
/user/update
/user/delete
/users
/users/:user_id
/profiles/:profile_id
/*
/

Route String Mappings

The following string mappings will be applied on all filenames to generate the eventual route:

{star}  -> *
{index} -> /
{_}     -> :

So a file named a{star}.js will correspond to the pathname /a*.

A file named {index}.js will be the root of the pathname, i.e. /.

A file named {_}id.js will correspond to the pathname /:id.

The Route File

All route files should export a function that returns either an object or an Expres middleware.

Middleware Export

A route file that exports a middleware will look similar to:

module.exports = () =>
  (req, res) => {
    res.json('ok');
  };

Given such a route file, express-diroutes will call the .use(...) method on the Express router.

If the file is named healthz.js and is located in a directory named a in the rootPath specified when instantiating express-diroutes, the following method will be called:

const router = new express.Router();
// ...
router.use('/a/healthz', require('%_ROOT_PATH_%/a/healthz')());

Object Export

A route file that exports an object should look similar to:

module.exports = () => ({
  get: (req, res) => {
    res.json('GET /...');
  },
  post: (req, res) => {
    res.json('POST /...');
  },
});

Given such a route file, express-diroutes will register the get property with the .get(...) method on the Express router and the post property with the .post(...) method on the Express router.

If the file is named test.js and is located in a directory named routes in the rootPath specified when instantiating express-diroutes, the following methods will be called:

const router = new express.Router();
// ...
router.get('/routes/test', require('%_ROOT_PATH_%/routes/test')().get);
router.post('/routes/test', require('%_ROOT_PATH_%/routes/test')().post);

In other words, the properties of the exported object should correspond to an available method on the object returned by new express.Router().

Contributing

Fork this repository, make your changes and submit a pull request to the master branch.