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-routes-controllers

v0.4.0

Published

Creates routes for the controllers

Downloads

6

Readme

express-routes-controllers

You can create routes for your controllers.

Install

npm install express-routes-controllers

How to use it:

It is very simple:

var Rest = require('express-routes-controllers');

var rest = new Rest( { controllers: __dirname + '/app/controllers' } );

Optinal headers to version:

var rest = new Rest( { controllers: __dirname + '/app/controllers',
                      versioning: { header: 'Accept',
                                    grab :/.*application\/vnd.mycompany.v(\d+)\+json/,
                                    error: '406' }
                    } );

And then, add the routes:

//your_controller.js
module.exports = {
  options: {
    before: {
      action: [/*middleware*/]
    }
  },
  index: function(req, res) {},
  another_action: function(req, res) {}
}

//app.js

var express = require('express');
var app = express();
var Rest = require('express-routes-controllers');
var rest = new Rest( { controllers: __dirname + '/app/controllers' } );

rest.resources('your_controller', options);
rest.resource('another_controller', options);

// ...all routes for your controllers.

//add the routes to express.
rest.mountRoutes(app);

Resources and Resource:

You can add the CRUD actions:

rest.resources('your_controller');

// GET '/your_controller'
// POST '/your_controller'
// PUT '/your_controller/:your_controller_id'
// DELETE '/your_controller/:your_controller_id'
// GET '/your_controller/:your_controller_id'

Or if you want to add them without the id on the url (removing the show action):

rest.resource('your_controller', options);

// GET '/your_controller'
// POST '/your_controller'
// PUT '/your_controller'
// DELETE '/your_controller'

Can also add custom actions with the CRUD actions:

rest.resources('your_controller', {
  collection: {
    get: [ 'collection_action' ]
  },
  member: {
    post: [ 'member_action' ]
  }
}

// GET /your_controller/collection_action
// POST /your_controller/:your_controller/member_action

To add support for just some versions:

rest.resources('your_controller', {
   versions: ['1']
  }
});

This would require a specific folder structure:
<controller's folder>/v1/your_controller.js


To support all versions, remove the version's option or use a wildcard

rest.resources('your_controller', { versions: ['*'] } });

Or just:

rest.resources('your_controller', {})

In both cases, it would work with default controller's folder: <controller's folder>/your_controller.js

Possible options:

  var options = {
                  versions: ['1','2'],
                  collection: {
                    get: [ /* Array of custom actions */ ],
                    post: [ /* Array of custom actions */ ],
                  },
                  member: {
                    get: [ /* Array of custom actions */ ],
                    post: [ /* Array of custom actions */ ],
                  },
                  name: 'Change the default name for the url. DEFAULT: name_of_controller',
                  id: 'The id variable on the ulr. DEFAULT: name_of_controller_id',
                  root: 'Boolean. DEFAULT: false'
                }

Nested controllers

You can set nested controllers, you only have to do:

rest.resources('your_controller', function(){
  rest.resources('another_controller');
});
// '/your_controller/:your_controller_id/another_controller'


And you also set versions in it and only for the nested:

rest.resources('your_controller', function(){
  rest.resources('another_controller', {versions: ['2']});
});

The required folder structure would be:
<controller's folder>/your_controller.js
<controller's folder>/v2/your_controller/another_controller.js

Change the url

If you have the user controller and you want that the url be '/account'

rest.resources('users', {
  name: 'account'
});

// GET '/account'
// POST '/account'
// PUT '/account/:account_id'
// DELETE '/account/:account_id'
// GET '/account/:account_id'