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-route-mapper

v0.0.7

Published

Route mapper middleware for Express

Downloads

49

Readme

express-route-mapper

Manage routes and aliases in Rails fashion.

Installation

$ npm install express-route-mapper

Example

Check out the example app in github repository to quickly get started

Features:

- Make route registration a manageable process by having routes registered in single registry file

- Create custom alias for route

- Retrieve route path from alias

- Retrieve alias from path

- Support RESTful API

Notice: If you register routes not using this module, retrieving route path or alias will return undefined value.

Usage

Include module and register all routes in app.js

  app.set('routemap', require('express-route-mapper'));
  app.get('routemap').map(app, 'config');

Setting routemap local variable for View to retrieve URI from path name and aliase (See later section):

  app.use(function(req, res, next){
    app.locals.routemap = app.get('routemap');
    req.routemap = app.locals.routemap;
    next();
  });

Here we first require the module and then specify the configuration file location the module should look into under the route directory. You could also define your own path for both configuration file and route register file. The following setting tells Node to look into $APP_DIR/routes/route_config_file.js for route configuration and $APP_DIR/routes/route_register_dir/ for all files that register route in your app.

  app.get('routemap').map(app, 'route_config_file', 'route_register_dir') 

Resource:

If you want to register routes for a resource then the syntax will look like this:

In routes/config.js

module.exports = [
  {resource: 'photos'}
]

when registering a route with resource, route mapper register all related routes following RESTful API.

The above map will register the following routes:

Path name | URI | Callbak/HTTP Method :--------------|-------------:|:------------------: list_photos | /photos | index [GET] show_photos | /photos/:id | show [GET] new_photos | /photos/new | new [GET] create_photos | /photos | create [POST] update_photos | /photos/:id | update [PUT] delete_photos | /photos/:id | delete [DELETE]

Then in routes/photos you need to define the appopreate callbacks. An example would look like this:

module.exports = {
  index: {
    callback: function(req, res) {
      res.send("List photos!");
    }
  }
}

Custom route name and alias

In Rails you can create route like following:

match: 'signin', to: 'session#new', via: get

Here we could do the similar things:

{get: '/signin', to: 'session#new', as: 'signin'},

The above registry will map a GET request to /signin path to the new callback defined in routes/session file.

Retrieve URI

Using path name or alias you can retrieve the URI which you could later change it without affecting the result. This allow you to reference to an URIs without worrying about later changes will break those links.

Give the above declaration, you could for example simply do things like the followings:

- In Route routes/users.js

show: {
param: ':user_id',
callback: function(req, res, next) {
    if(req.session.userId != req.user_id) {
        return res.redirect(routemap.get('signin');
    }
    else {
    // Do stuff
    }
}

Retrieve alias The following code shows how to get alias from route path:

  app.get('routemap').alias('/photo/:photo_id', 'get'); // show_photo
  app.get('routemap').alias('/photos', 'post'); // create_photo

#License

(The MIT License)

Copyright (c) 2013 Tran Khanh <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.