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

sans-server-middleware

v1.4.5

Published

A sans-server hook runner.

Downloads

36

Readme

Build Status Coverage Status

sans-server-middleware

A sans-server hook runner.

This package is used by sans-server and sans-server-router to enact hooks.

Example

const Middleware = require('sans-server-middleware');
const SansServer = require('sans-server');

const middleware = new Middleware();

// add normal middleware
middleware.add(function a(req, res, next) {
    req.str = 'a';
});

// add normal middleware - produces error
middleware.add(function b(req, res, next) {
    req.str += 'b';
    next(Error('Failure'));
});

// add normal middleware - skipped because of error before
middleware.add(function c(req, res, next) {
    req.str += 'c';
});

// add error middleware - resolves error
middleware.add(function d(err, req, res, next) {
    req.str += 'd';
    next();
});

// add normal middleware
middleware.add(function e(req, res, next) {
    req.str = 'e';
});

// run the middleware
SansServer.use(function(req, res, next) {
    middleware.run(req, res, next);
});

Middleware constructor

This constructor is used gather and run hooks that are added to it.

Signature Middleware() : Middleware

Methods

  • add - Add a hook function.
  • from - Add an array-like or iterable of hook functions.
  • reverse - Run the added middleware functions in reverse order.
  • run - Run the added middleware functions.

Properties

  • length - Get the number of hooks attached to the instance.

Parameters None

Returns a middleware instance.

Example

const Middleware = require('sans-server-middleware');

const middleware = new Middleware();

middleware.add(function myHook(req, res, next) {
    // do something ...
    next();
});

Middleware#add

Add a middleware hook.

Signature Middleware#add( [ weight, ] hook ) : undefined

Parameters

| Parameter | Description | Type | Default | | --- | --- | --- | --- | | weight | A number that represents the weight of the function to add. A hook with a lower weight will run sooner than a hook with a higher weight. | number | 0 | hook | A middleware function. Naming the function will improve log readability. | function | |

Returns undefined

Example

const Middleware = require('sans-server-middleware');

const middleware = new Middleware();

middleware.add(function myHook(req, res, next) {
    // do something ...
    next();
});

Middleware#from

Add middleware from an array-like object or an iterable. Each item must be a function.

Signature Middleware#from( iterable ) : undefined

Parameters

| Parameter | Description | Type | Default | | --- | --- | --- | --- | | iterable | An array-like object or iterable | Array.<function> | |

Returns undefined

Example

const Middleware = require('sans-server-middleware');

const middleware = new Middleware();

const ar = [];
ar.push(function myHook(req, res, next) {
    // do something ...
    next();
});

middleware.from(ar);

Middleware#length

Get the number of hooks that have been added to the instance.

Signature Middleware#length : number

Type number

Example

const SansServer = require('sans-server');
const Middleware = SansServer.Middleware;

const middleware = new Middleware();

middleware.add(function(req, res, next) {
    // do something ...
    next();
});

console.log(middleware.length);     // 1

Middleware#reverse

Run through the added hooks in reverse order.

Signature Middleware#reverse( req, res [, next ]) : Promise|undefined

Parameters

| Parameter | Description | Type | Default | | --- | --- | --- | --- | | req | The sans-server request object | Request | | | res | The sans-server response object | Request | | | next | A function to call once all hooks have been run. It will receive an Error as a parameter if there was an unresolved error while running the middleware. If this function is omitted then a promise will be returned | function | undefined |

Returns a Promise if the next parameter was not provided. The promise resolves if there are no unresolved errors while running the middleware, otherwise it rejects with the error reason. If the next parameter was provided then undefined is returned instead.

Example

const SansServer = require('sans-server');
const Middleware = SansServer.Middleware;

const middleware = new Middleware();

middleware.add(function(req, res, next) {
    // do something ...
    next();
});

const server = new SansServer();
server.use(function(req, res, next) {
    middleware.reverse(req, res, next);
});

Middleware#run

Run through the added hooks in order.

Signature Middleware#run( req, res [, next ]) : Promise|undefined

Parameters

| Parameter | Description | Type | Default | | --- | --- | --- | --- | | req | The sans-server request object | Request | | | res | The sans-server response object | Request | | | next | A function to call once all hooks have been run. It will receive an Error as a parameter if there was an unresolved error while running the middleware. If this function is omitted then a promise will be returned | function | undefined |

Returns a Promise if the next parameter was not provided. The promise resolves if there are no unresolved errors while running the middleware, otherwise it rejects with the error reason. If the next parameter was provided then undefined is returned instead.

Example

const SansServer = require('sans-server');
const Middleware = SansServer.Middleware;

const middleware = new Middleware();

middleware.add(function(req, res, next) {
    // do something ...
    next();
});

const server = new SansServer();
server.use(function(req, res, next) {
    middleware.reverse(req, res, next);
});