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-routeloader

v2.0.0

Published

Automatic routeloader for the express web framwork

Downloads

12

Readme

Express-routeloader

travis NPM version Codeship Status for cullophid/express-routeloader

This is originally a fork of Livedocs-routeloader. Some credit goes to Simon Mcmannus

Basics

Express-routeloader makes it easy to handle endpoints for your REST server. Express-routeloader builds an express router from your ./routes folder.

when given a directory structure like

routes
    - groups.js

where groups is

exports.read = {
    handler : function (req, res) {
        res.send(200);
    }
};
exports.create = {
    handler : function (req, res) {
        res.send(200);
    }
};

Express-routeloader will create a router with the following routes:

POST /groups
GET /groups/:id

You can use multiple routers in the same project which is really useful for maintaining multiple versions of an api.

Routeloader

The routeloader works as a simple router of express:

'use strict';

var express = require('express'),
    app = express(),
router = require('express-routeloader')({/* option */});

app.use(router);

app.listen(3000);

options

rootDir: The directory containing your routes. Defaults to ./routes

logger: Logging function. default : console.log

hideCRUD : Should CRUD endpoints have the extentions be hidden. GET assets/read/:id becomes `GET assets/:id. Defaults to true.

verbMap: Json object mapping filenames to default HTTP verbs. default :

{
    "create": "POST",
    "read": "GET",
    "list" : "GET",
    "search" : "GET",
    "update": "PUT",
    "delete": "DELETE"
}

prefix: prefix for all loaded routes e.g. /api/v1/. this is very useful for maintaining multiple versions of an api.

Routes

routing endpoints are node modules that export a set of objects with a method handler:

'use strict' // Ofc we are running strict!
exports.hello = {
    handler :function (req, res, next) {
        res.send('world');
    }
};

You can overwrite default settings by adding properties to the exported object:

'use strict' // Ofc we are running strict!

exports.hello = {
    url = '/different/route/to/:id',
    method = 'PUT',
    middleware = [func1, func2, func3],
    handler : function (re, res, next) {
        res.send('World');
    }
};

validation

Express-routeloader lets you use json-schema to validate the input to your endpoints. to add validation simply add the schema to the module.

'use strict' // Ofc we are running strict!

exports.hello = {
    url = 'update/:id',
    method : 'POST',
    params =  {
        required : ['id'],
        properties : {
            handler : {
                type : 'integer',
                minimum : 0
            }
        }
    },
    query = {
        properties : {
            someOption : {
                type : 'boolean'
            }
        }
    },
    body = {
        properties : {
            name : {
                type : 'string',
                maxLength : 80
            }
        }
    },
    handler : function (req, res, next) {
        // do an update
        res.send('world');
    }
};

Promises

Express route validator supports promises. If a route handler returns a promise, routeloader with call res.send on promise resolution and next on rejection.