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

otter-routes

v1.0.6

Published

Directory convention based ExpressJS route builder

Downloads

25

Readme

Otter Routes Build Status

Otter Routes is an Express route builder based on directory structure, with controllers exporting a route callback. The method comes from a verb in either the function name on the controller, or the file name.

$ npm install otter-routes

By default, the verbs map directly to the same method name:

  • Get -> get
  • Put -> put
  • Post -> post
  • Delete -> delete

The following properties are key words when exporting from a controller:

  • controller -> express route callback - function ControllerName(req, res) {};
  • menu -> will return an object with all properties, as well as add controllerName, method, and path
  • urlPattern -> overridable string for the url, in case you need additional route params
  • permission -> name of the permission required to access this route, will be added to the menu as well
  • permissionMiddleware -> express callback for permission to access the route. function(req, res, next){ next(); })

All other non function properties exported will be returned in the final callback as an array with the objects listed. All other function properties exported will create an additional route appended to the controller's path with the same method.

The following options are available:

  • methods -> Map a verb to a http method. ie: {'update': 'put'}
  • diagnostics -> Add /_otter/routes to your app to display all routes built by otter
  • idParamMatchers -> Array of verbs to append /:id to the route string
  • parentIdParamMatch -> Matches end of controller name to append /:parentid after the parent directory in the route string. Defaults to ForParent.

Most basic example

This example, with no config, will read the controllers directory in the cwd for routes

var express = require('express'),
    otter = require('otter-routes'),
    app = express();

otter(app, function(err, results) {
    //results will have all routes as well as all additional properties exported from your controllers
    //like results.menu, or results.custom if you add your own custom property

    app.listen(3001, function() {
        console.log('app listening on port 3001');
    });
});

More advanced example

var express = require('express'),
    otter = require('otter-routes'),
    app = express();

var config = {
    idParamMatchers: ['view', 'update', 'destroy'],
    diagnostics: true,
    methods: {
        'get': 'get',
        'put': 'put',
        'post': 'post',
        'delete': 'delete,

        'list': 'get',
        'view': 'get',
        'update': 'put',
        'create': 'post',
        'destroy': 'delete'
    }
};

/*
    For every get, put, post, or delete verb in the controller/file name, the express method will map directly to the
    verb.
    List will map to get, with no parameters
    View will map to get, with /:id parameter
    Update will map to get, with /:id parameter
    Create will map to post, with no parameters
    Destroy will map to delete, with /:id parameter
*/

otter(app, config, function(err, results) {
    app.listen(3001, function() {
        console.log('app listening on port 3001');
    });
});

Controller Examples

If the following controller were placed in GetUsers.js in the directory users, it would create a get method on the route /users. (app.get('/users');)

If more options are needed for the controller, you must use exports.controller

module.exports = function GetUsers(req, res) {};

or

exports.controller = function GetUsers(req, res) {};

Override exports.urlPattern This will no longer create a get method at the path /users, but will make a get method at the path /path/to/new/list/of/users

exports.urlPattern = '/path/to/new/list/of/users';

exports.controller = function ListUsers(req, res) {
    //database call
    res.json(users);
};

Set exports.menu. the final callback of otter will return results.menu with an array of menu items. results.menu [ { title: 'Home', //added in the exports.menu declaration controllerName: 'GetHome', //added automatically by otter method: 'get', //added automatically by otter path: '/home' //added automatically by otter } ]

exports.menu = {
    title: 'Home'
};

exports.controller = function GetHome(req, res) {
    res.json({path: '/home'});
});

Custom function: we have an aggregate service that will group lists by specified properties, instead of building and entire new route, we just use exports.aggregate as an express route callback to append /aggregate to the original list's path. For our aggregate we use a service,

If the following file were placed in the users directory, it would create a get method at /users to list all users, as well as a get method at /users/aggregate.

var aggregateService = require('path/to/aggregateService');

exports.controller = function ListUsers(req, res) {
    //database call
    res.json(users);
};

exports.aggregate = aggregateService('Users');