1.4.5 • Published 7 years ago

sans-server-middleware v1.4.5

Weekly downloads
2
License
Apache-2.0
Repository
-
Last release
7 years ago

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

ParameterDescriptionTypeDefault
weightA 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.number0
hookA 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

ParameterDescriptionTypeDefault
iterableAn array-like object or iterableArray.<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

ParameterDescriptionTypeDefault
reqThe sans-server request objectRequest
resThe sans-server response objectRequest
nextA 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 returnedfunctionundefined

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

ParameterDescriptionTypeDefault
reqThe sans-server request objectRequest
resThe sans-server response objectRequest
nextA 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 returnedfunctionundefined

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);
});
1.4.5

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago