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

njet-routing

v1.1.3

Published

njet-routing - router for any http server

Downloads

5

Readme

njet-routing Build Status

###INSTALLATION

You need Node.js and npm installed. Then you can install njet-routing using this command:

npm install njet-routing

###REQUEST METHODS

var njetRouting = require('njet-routing'),
    router = njetRouting.createRouter();

You can use following methods methods:

  • get
  • post
  • put
  • delete
  • options
  • head
  • trace
  • connect
  • any (custom, when request method is not specified)

###ADD ROUTE

To add route for any verb use router.{method}.add() like this:

router.post.add('create_user', '/user/{type}')

where type is variable passed by user. Default regexp for any variable is ([^/]+). Variable can have default value. Define it like this:

router.post.add('create_user', '/user/{type | super me}')

If you need specific pattern for "type", add it to third parameter - requirements:

router.post.add('create_user', '/user/{type | super me}', {
    type: '[a-z]+'
})

Remember, default value will be trimmed to "super me" and it will be not affected by provided requirement for type.

Now if "type" is not provided when generating url, default value will be "super me".

Also, each route can carry data object. For example while registering route, you want to keep some informations, like controller name that should handle request or anything like that. If you want to have that information, simply add 4th parameter to .add() method like this:

router.post.add('create_user', '/user/{type | super me}', {
    type: '[a-z]+'
}, {
    myControllerName: 'MySuperController',
    myActionName: 'CoolAction',
    randomStuff: 'my dog name is Cesar'
})

###GET ROUTE

To retreive route, use get() method:

var route = router.post.get('create_user')

You will have full object with all data you provided so far.

###GENERATE PATH

To generate url based on route name and arguments, use:

var route = router.post.generate('create_user', {
    type: 'superman',
    age: 26
})

Any unused parameter will be added to url as query string. In this specific case it will be like this:

/user/superman?age=26

If type is omitted while default parameter is defined, default parameter will be used. Default argument is add as alternative to regexp in requirements so both will always match.

If you need absolute url, set third argument to true:

var route = router.post.generate('create_user', {
    type: 'superman',
    age: 26
}, true)

This will generate:

http://localhost/user/superman?age=26

Remember that all arguments added to query string are sorted alphabetically so order on argument list does not matter.

To change scheme, base url or host, use:

  • setHost()
  • setScheme()
  • setBaseUrl()
  • setPort()

Like this:

router.setScheme('https').setHost('dariuszp.com').setBaseUrl('my/new');
var route = router.post.generate('create_user', {
    type: 'superman',
    age: 26
}, true)

This will generate:

https://dariuszp.com/my/new/user/superman?age=26

Ad port as 4th parameter in case you want to change port for just this route:

router.post.generate('create_user', {
    type: 'superman',
    age: 26
}, true, 8983);

This will generate:

https://dariuszp.com:8983/my/new/user/superman?age=26

Default port 80 is never added to route for obvious reasons. If You want port to be always visible, you can force it:

.forcePortInUrl(true)

Also if all your routes should have custom port, simply use:

.setPort(8983); // for example port 8983

###MATCHING

To find out if your path match any route, use match for any method (verbs):

router.post.match('/user/superman?age=26');

.match() return either false or result object. Result object have properties:

  • route - route object
  • routeParams - route in path extracted from given pattern
  • queryParams - params from query string
  • params - all params but be aware that if route param name match query param name, route param takes priority

By aware that match will check either specific verb routes or all routes. ANY is just another group of routes. Matching will not check any at any point. Programmer need to do it himself/herself.

###DEBUGGING

To get all routes, use .dump(method = false, byName = false) method. Dump accept two arguments:

  • method - dump only specific methods
  • byName - dump routes with names as keys