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

v2.1.1

Published

Declarative Application Routing

Downloads

17

Readme

Build Status Test Coverage Code Climate Dependency Status devDependency Status

NPM

express-jefferson

Declarative Express Application Wiring

express-jefferson is a library for declaratively describing RESTful services and configuring express apps.

// main.js
var express = require('express'),
    jefferson = require('express-jefferson'),
    app = express(),    
    conf = {
        proxies: [logInvocations, resolvePromises],
        pre: {
            safe: [markReadOnly],
            unsafe: [authenticate]
        },
        post: {
            all: [addHaetoasLinks, transmitResponse]
        },
        params: {
            beerId: getBeerById,
            userId: getUserById
        },
        routes: {
            '/beers': {
                get: [getBeerList, send.json]
                post: [createBeer, send.json]
            },
            '/beers/:beerId': 
                get: [send.json]
                put: [editBeer, send.json]
            }
        }
    };
    
jefferson(app, conf);

Configuration

  • routes - (required) - A map of routes by route path. Each object in the map contains a map of method to middleware function array.
routes: {
    {
        '/my-path': {
            get: [getThings, send]
            past: [makeThing, send]
        },
        '/my-path/:id': {
            get: [getThing, send]
        }
    }
}
  • aliases: (optional) - A map of alias-name to handler chain. Routes may use these aliases in lieu of repeated function groups.
aliases: {
    'processAndTransmit': [deletePassword, addHateoasLinks, transmitEntity]
},
routes: {
    '/users/:id': {
        get: [getUser, 'processAndTransmit']
    }
}
  • pre: (optional) - (object) Boilerplate section of pre-middleware functions (see below)
  • post: (optional) - (object) Boilerplate section of post-middleware functions (see below)
  • proxies: (optional) - An array of proxy objects invoked around all middleware functions in order. (see below)
  • params: (optional) - A map of path-parameter name to resolver functions.
params: {
    userId: (req, res, next) => {
        req.user = userStore.findUser(req.params.userId);
        next();
    }
}
routes: {
    '/users/:userId': {
        get: [(req, res, next) => res.send(req.user)]
    }
}
  • enable: (optional) - An array of settings strings to enable (http://expressjs.com/api.html#app.enable)
enable: ['trust proxy', 'etag']
  • disable: (optional) - An array of settings strings to disable (http://expressjs.com/api.html#app.disable)
disable: ['trust proxy', 'etag']
  • engines: (optional) - An map of template rendering engines. { <extension>: <engineFunction> }
engines: {
    'jade': require('jade').__express
}
  • locals: (optional) - (object) An object that will populate app.locals (http://expressjs.com/api.html#app.locals) { <localName>: <localValue> }
locals: {
    'a': true
}
  • settings: (optional) - (object) An object that will be iterated to populate application settings using app.set (http://expressjs.com/api.html#app.set) { <settingName>: <settingValue> }
settings: {
    'a': true
}

###Boilerplate Config Sections (pre/post):

  • all: (optional) - An array of middleware to be applied to all endpoints.
  • safe: (optional) - An array of middleware to be applied to all safe endpoints (GET, HEAD, OPTIONS).
  • unsafe: (optional) - An array of middleware to be applied to all unsafe endpoints (not GET, HEAD, OPTIONS).
  • method: (optional, object) - On object of method-name to handler list.
pre: {
    all: [trackInvocation],
    unsafe: [authenticate],
    method: {
        get: [markRequestReadOnly]
    }
}

###Proxy Definition:

  • name: - The name of the proxy.
  • init: - A function that accepts a middleware function, augments it, and returns the augmented proxy.
  • conf: (optional) - A configuration object that is passed to the proxy init function.
{
    name: 'Logger',
    init (delegate) {
        return (req, res, next) => {
            console.log("invoking middleware function");
            delegate(req, res, next);
        }
    }
}

Proxies provided in Jefferson

Promise-Based Middleware Proxy

require('express-jefferson/proxies/promise-handler')

This proxy accepts promise-based middleware (middleware that accepts two arguments) and wraps them in a promise chain before invoking next().

Config Options:

  • haltCondition (optional) - a predicate function that accepts the request and response. If it returns a truthy value, then the middleware chain will be halted.

Trace Logger Middleware Proxy

require('express-jefferson/proxies/trace-logger')

This proxy logs invocations of middleware using the debug library.

Config Options:

  • logger (optional) - the name of the debug logger to use. Default is "jefferson:trace-logger"