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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-group-router

v2.1.1

Published

Extend the router function in ExpressJS for grouping route with prefix and add midddlewares for each route.

Downloads

67

Readme

Features:

This package support extend function of Router in ExpressJS to grouping routes.

  • It can set prefix for an routes group.
  • It can set middlewares for an routes group.

Installation:

npm i --save express-group-router

Usage:

Version 2.x

const express = require('express');
const app = express();
const Router = require('express-group-router');
let router = new Router();
const fooMiddleware = (req, res, next) => {
    console.log('foo');
    next();
}

const barMiddleware = (req, res, next) => {
    console.log('bar');
    next();
}
router.get('/hello', (req, res) => {
    res.send('Hello world');
})

router.group('/foo', [fooMiddleware], (router) => {
    router.get('/a', (req, res) => {
        res.send('Foo');
    });

    router.group('/bar', [barMiddleware], (router) => {
        router.get('/test', (req, res) => {
            res.send('Test Bar');
        })
    })
});
let listRoutes = router.init();
app.use(listRoutes);

You also use prefix(), middleware() as below to set prefix, set middlewares and options for router of ExpressJS;

// Set prefix
router.group((router) => {
    router.get('testprefix', (req, res) => {
        res.send('Test function set prefix');
    })
}).prefix('api').middleware(fooMiddleware);
// Result: /api/testprefix - method GET with middleware fooMiddleware.

In ExpressJS Router (The original Router of Express, it provide options argument when call constructor) You can refer from https://expressjs.com/en/api.html And this package also provide options

const Route = require('express-group-router');
let router = new Route({ options: { caseSensitive: true, mergeParams: false, strict: true }});
// Or 
router.setOptions({
    caseSensitive: false
});

// or
router.param('id', (req, res, next) => {
    // do something...
    req.object = 'object value';
})
router.group((router) => {
    router.get('/value/:id', (req, res) => {
        res.send(req.object);
    });
}).prefix('test').setOptions({ mergeParams: true });
// route: /test/value/:id return 'object value' on browser.
// To convinient, the mergeParams default value is true;
// If the parent and the child have conflicting param names, the child’s value take precedence.

// if mergeParams = false => route /test/value/:id will return undefined;
router.group({ prefix: 'test2', options: { mergeParams: false }}, (router) => {
    router.get('/:id', (router) => {
        res.send(req.object); // return undefined;
    })
});

Version 1.x

const express = require('express');
const app = express();
require('express-group-router')(app);
const fooMiddleware = (req, res, next) => {
    console.log('foo');
    next();
}

const barMiddleware = (req, res, next) => {
    console.log('bar');
    next();
}
let router = express.Router();
router.group({ prefix: '/api', middlewares: [fooMiddleware]}, (router) => {
    // route: /api/hello-world, middlewares: fooMiddleware
    router.get('/hello-world', (req, res) => { 
        res.send('Hello world');
    })

    router.group({prefix: '/test', middleware: barMiddleware }, (router) => {
        // route: /api/test/group; Middlewares: fooMiddleware, barMiddleware;
        router.get('/group', (req, res) => { 
            res.send('Test group');
        })
    })

    router.group([barMiddleware], (router) => { // not add sub prefix
        // route: /api/not-prefix
        router.get('/not-prefix', (req, res) => { 
            res.send('Not prefix');
        });
    })

    router.group('/prefix' ,[barMiddleware], (router) => { // not add sub prefix
        // route: /api/prefix/alert
        router.get('/alert', (req, res) => { 
            res.send('Not prefix');
        });
    })

    router.group('/foo', (router) => { // not add middleware more..
        // route: /api/foo/bar
        router.get('/bar', (req, res) => { 
            // Middlewares: fooMiddleware
            res.send('Foo bar')
        })
    })
})

Buy me a coffee

If you like this library, buy me a coffee, thanks!

Vietnam Bank transfer