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

recursive-routing

v1.2.1

Published

Recursively configure Express.js routes in a folder structure

Downloads

8

Readme

recursive-routing

Configure Express.js routes recursively using a directory structure. Very easy to configure and use.

Inspired in express-recursive-routes by @megadix.

Npm package license Npm package version Npm package monthy downloads

Installation

Installation is done using the npm install command:

$ npm i --save recursive-routing

Usage

As @megadix explained in express-recursive-routes, others libraries like express-generator creates routes under a /routes directory, but you need to manually register every route like this:

var index = require('./routes/index');
var users = require('./routes/users');

app.use('/', index);
app.use('/users', users);

And it's very annoying to have to do this for every route. I mean, it literally takes a lot of time if you have a lot of routes.

✨ And that's why this package exists ✨

express-recursive-routes is a great library, but it doesn't allow you much freedom and customization to configure routes. And also uses deprecated features with newer versions of Node.js.

With recursive-routing, you can let the library do the heavy lifting for you. It will create routes recursively, and you can configure the routes with a directory structure.

Just do:

const express = require('express');
const app = express();

const recursiveRouting = require('recursive-routing');

recursiveRouting(app);

app.listen(3000);

Then under routes directory, you can create a directory structure like this:

routes/
├── index.js
├── users/
│   ├── index.js
│   └── profile.js

And, by example, in /routes/index.js, you can create a route like this:

const express = require('express');
const router = express.Router();

router.get('/', function(req, res) {
	res.send('Hello World!');
});

module.exports = router;

And voila! You have a fully configured Express.js application.

recursive-routing translates every directory into a route, by example, /routes/users/profile.js will be translated to /users/profile, and /routes/users/index.js will be translated to /users.

And what if I need to use routes with different HTTP methods?

Well, recursive-routing supports it too.

const express = require('express');
const router = express.Router();

router.get('/', function(req, res) {
	res.send('Hello World from GET!');
});

router.post('/', function(req, res) {
	res.send('Hello World from POST!');
});

// And also supports routes with parameters!
router.get('/:id', function(req, res) {
	res.send(`Hello World from GET with id: ${req.params.id}`);
});

module.exports = router;

Configuration

recursive-routing exports a function that can be used to configure the routes, and the function parameters are:

recursiveRouting(app: express.Application, options?: RecursiveRoutingOptions): void

The configuration is done by passing an object to recursive-routing function.

const express = require('express');
const app = express();

const recursiveRouting = require('recursive-routing');

recursiveRouting(app, {
	'rootDir': './api-routes',
	'routePrefix': '/api',
	'replaceSpacesWith': '_'
});

app.listen(3000);

The options of recursive-routing are:

| Option | Type | Default | Description | | ------------------- | --------------------------------------------------- | ------------------------------------------------------------ | ---------------------------------------------------------------------------- | | rootDir | string | './routes' | The root directory of routes. | | basePath | string | '/' | The base path of routes. | | filter | function(string) | f => f.endsWith('.js') | A function that returns true if the file should be included in the routes. | | mountFunction | function(express.Application, data, express.Router) | (app, data, router) => app.use(data.expressRoutes, router) | A function that mounts the routes. | | replaceSpacesWith | string | '-' | The string that will be used to replace spaces in the route path. | | keepExtension | boolean | false | If true, the extension of the file will be kept. | | keepIndex | boolean | false | If true, the index.js file will be kept as /index too. | | debug | boolean | false | If true, the debug messages will be printed. |

In the mountFunction function, there's a parameter data that contains the data of the route. It's an object with the following properties:

| Name | Type | Description | | --------------- | -------- | ---------------------------------------- | | route | string | The path of the found file. | | routePath | string | The path of the file with the base path. | | routeName | string | The name of the file without extension. | | fullPath | string | The full path of the file. | | expressRoutes | string[] | The array of Express.js routes. |

And that data will be printed to the console if debug is true.

Examples

Simple routing

const express = require('express');
const app = express();

recursiveRouting(app, {
	'rootDir': './api-routes',
	'routePrefix': '/api',
	'replaceSpacesWith': '_'
});

app.listen(3000);

Website routing with REST API

const express = require('express');
const app = express();

recursiveRouting(app, {
	'rootDir': './api-routes',
	'routePrefix': '/api',
	'filter': f => f.endsWith('.js') || f.endsWith('.ts'),,
	'mountFunction': (app, data, router) => {
		console.log(data);

		for (var i in data.expressRoutes) {
			data.expressRoutes[i] = data.expressRoutes[i].replace(/^\/api/, '');
		}

		app.use(data.expressRoutes, router);
	}
});

recursiveRouting(app, {
	'rootDir': './website'
});

app.listen(443);

La macarena

const ejs = require('ejs');

const express = require('express');
const app = express();

app.set('view engine', 'ejs');
app.use(express.static('public'));

app.get('/sing-a-song/', (req, res) => {
	res.render('sing-a-song', {
		'name': 'La macarena'
	});
});

recursiveRouting(app, {
	'rootDir': './views',
	'routePrefix': '/views'
});

app.listen(443);

Contributing

This project is open-source and you can contribute to it by opening an issue or creating a pull request.

License

This project is licensed under the MIT license.