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

@ash-framework/middleware

v1.0.0

Published

Middleware support for ash framework

Downloads

6

Readme

Middleware support for ash framework

NPM

Usage

Loads middleware from a specified directory or from inline express style middleware functions.

Defining middleware routing

Basic routing

Define a nested function to specify which middleware to load and in what order.

const middleware = function () {
  this.middleware('token')
  this.middleware('user')
}

In the example above, token middleware will be loaded before user middleware.

this.middleware

this.middleware(name|express middleware, [options|callback])

The this.middleware function takes the name of the middleware as the first parameter (or alternatively an express middleware function can be passed to bypass middleware loading and be used instead See Inlining middleware) The second optional parameter is either an options object to be passed to the middleware to be loaded (See Loading middleware) or a callback function to be used for grouping middleware. (See Grouping middleware)

Grouping middleware

Middleware can be grouped using nesting. This has no application other than to help you to group your middleware in a way that makes sense for your application.

const middleware = function () {
  this.middleware('security', function () {
    this.middleware('token')
    this.middleware('csrf')
  })
  this.middleware('user')
}

In the above example, token middleware will run followed by csrf and then finally user. No middleware is run for security itself as this is just a namespace.

Inlining middleware

The this.middleware function can also take an express style middleware callback of the form function (req, res, next) {} The main intended use for this is so that you can load external middleware.

const helmet = require('helmet')

const middleware = function () {
  this.middleware(helmet())
}

or simply

const middleware = function () {
  this.middleware(function (req, res, next) {
    // do something
    next()
  })
}

Loading middleware

Loading middleware involves passing a definition function (See defining middleware routing), an express app instance and a path to a directory of route files to load from.

const express = require('express')
const load = require('@ash-framework/middleware')
const path = require('path')

const middlewareDefinition = function () {
  this.middleware('token')
}
const expressApp = express()
const middlewareDirectory = path.join(__dirname, 'middleware')

load(middlewareDefinition, expressApp, middlewareDirectory)

Defining middleware

When this.middleware(name) is called, the module will attempt to look up a file named name in the directory defined in middlewareDirectory (See Loading Middleware).

const app = express()

load(function () {
  this.middleware('user')
}, app, __dirname + '/middleware')

In the example above, a file name user.js will be looked for and loaded if it exists. (A descriptive error will thrown if it does not).

Loaded files should export a class with a method register

// user.js
module.exports = class Middleware {
  register (httpContext, options) {
    // return a promise if you want subsequent middleware to wait
    return Promise.resolve()
  }
}

httpContext

httpContext is a wrapper for express request and response objects. It is an object with 4 properties/methods as follows.

  • request - express request object
  • response - express response object
  • get(key) - property getting
  • set(key, value) - property setter

If you set something on httpContext in one middleware class, it will be available via get in later middleware classes.

const app = express()

const definition = function () {
  this.middleware('token')
  this.middleware('user')
}
load(definition, app, __dirname + '/middleware')

// token.js
module.exports = class Middleware {
  register (httpContext, options) {
    httpContext.set('token', 'ABC1234')
  }
}

// user.js
module.exports = class Middleware {
  register (httpContext, options) {
    const token = httpContext.get('token') // ABC1234
  }
}

options

options is an object passed through from the middleware definition

const app = express()

const definition = function () {
  this.middleware('token', {type: 'csrf'})
}
load(definition, app, __dirname + '/middleware')

// token.js
module.exports = class Middleware {
  register (httpContext, options) {
    console.log(options.type) // csrf
  }
}

Discover the release history by heading on over to the releases page.

These amazing people are maintaining this project:

These amazing people have contributed finances to this project:

Become a sponsor!

These amazing people have contributed code to this project:

Unless stated otherwise all works are:

and licensed under: