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-named-router-url-generator

v1.0.9

Published

Addition named router, URL generator for express route

Downloads

3

Readme

Expressjs Named Router & URL Generator

A lightweight express-js router wrapper with additional named router and URL generator.

If you're looking for Expressjs Named Router and URL Generator, here's your

Note: This is a wrapper - not a replacement

Features

  • All features of express router (http://expressjs.com/4x/api.html#router)
  • Naming router
  • URL generation
  • Router param matching conditional
  • Nested group (replace to router mouting by using app.use('/path', router))
  • Simple, easy to use, just need to change 2 lines of code to get this.
  • Easy to switch back to express router (if you don't like)

Installation

npm install express-named-router-url-generator --save

Requirements

  • Epxressjs 4.x

Usage

This is how you use express route (default)

var express = require('express');
var router = express.Router();
// var router = express.Router([options]); // with options

// router.get() or any methods that express router supports
router.get('/users/:id/:name', function(req, res, next) {
    res.send('user id is ' + req.params.id + ' and name is ' + req.params.name);
});

module.exports = router;

Just need to change 2 lines to use this module.

Naming Route (optional)

var express = require('express');
var router = require('express-named-router-url-generator')(express); // you need to change this line (1)
// var router = require('express-named-router-url-generator')(express[, options]); // with options

// router.get() or any methods that express router supports
router.get('/users/:id/:name', function(req, res, next) {
    res.send('user id is ' + req.params.id + ' and name is ' + req.params.name);
}
// this is optional argument
, { name: 'router.name' }
);

module.exports = router.expressRouter; // you need to change this line (2)

Route Matching with conditions (optional)

router.put('/users/:id/:name', function(req, res, next) {
    res.send('user id is ' + req.params.id + ' and name is ' + req.params.name);
}, {
    // optional naming
    name: 'router.name',
    // optional condition
    where: {
        id: router.NUMBER,
        name: router.ALPHA
    }
});

This module ships with some rules to use quickly.

proto.NUMBER = /\d+/;
proto.ALPHA = /[a-zA-Z]+/;
proto.ALPHA_NUMBER = /[a-zA-Z0-9]+/;
proto.ALPHA_NUMBER_DASH = /[a-zA-Z0-9_]+/;
proto.SLUG = /[a-zA-Z0-9\-]+/;
proto.SLUG_PLUS = /[a-zA-Z0-9_\-\+]+/;

Of course, you also can define your rules by using regex, array, or string

router.get('/:param1/:param2/:param3/:param4?', function(req, res, next) {
    res.send('hello word');
}, {
    name: 'route.name',
    where: {
        param1: /\d+/, // using regex
        param2: ['hello', 'world'], // match "hello" or "world"
        param3: 'fixed-string' // must equal to "fixed-string"
    }
});

URL Generation

Once you have defined your routes (and loaded the routes), to genrate URL, you can use router.urlFor method of router instance or urlFor helper provided by this module.

Both way are same and can use in any controllers.

router.urlFor

router.urlFor(name [, params [, query [, absolute]]]);

var link = router.urlFor('route.name', {id: '123456', name: 'this-is-a-name'});
console.log(link);

// with query string
router.urlFor('route.name', {id: '123456', name: 'this-is-a-name'}, 'a=b&c=d');
// or
router.urlFor('route.name', {id: '123456', name: 'this-is-a-name'}, {a: 'b', c: 'd'});

urlFor helper

var urlFor = require('express-named-router-url-generator').urlFor;

var link = urlFor('route.name', {id: '123456', name: 'this-is-a-name'});
console.log(link);

// with query string
urlFor('route.name', {id: '123456', name: 'this-is-a-name'}, 'a=b&c=d');
// or
urlFor('route.name', {id: '123456', name: 'this-is-a-name'}, {a: 'b', c: 'd'});

By default, all generated urls are relative. If you want to generate absolute URL, you need to use domain options.

var express = require('express');
var router = require('express-named-router-url-generator')(express, { domain: 'http://yourdomain.com' });
...

// param and query may be null.
router.link('route.name', null, null, true);

Note: If you mount a router to a path with app.use('/path/', router); URL generator will not know that so it will generate URLs that does not include this path. To define multiple routes inside a path as prefix, this module provide new method router.group()

Nested Route Group

This is helpful method to group multiple routes to a path (a replacement for mouting router by app.use('/path/', router))

router.group('/group1', function() {
    router.get('/path1', function(req, res, next) {
        res.send('you are in "/group1/path1"');
    });
    router.get('/path2', function(req, res, next) {
        res.send('you are in "' + router.link('route2') + '"'); // /group1/path2
    }, {
        name: 'route2'
    });
    router.group('/group2', function() {
        router.get('/path1', function(req, res, next) {
            res.send('you are in "' + router.link('route3') + '"'); // /group1/group2/path1
        }, {
            name: 'route3'
        });
    });
});

Change logs

version 1.0.8 - 2016/01/05

  • Fixed compile pattern
  • Support node 0.12.x
  • Added more pre-rules