1.0.1 • Published 6 years ago

express-map-middleware v1.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

express-map-middleware

Write functions instead of middleware

Why express-map-middleware?

The Express framework features the ability to assign an array of middlewares to a route. This ability allows you to compose your route logic as sequence of middleware rather than a single thick controller.

However this can lead to writing many small pieces of middleware or coupling disparate logic into a single piece of middleware.

express-map-middleware allows you to write single purpose functions that set or transform values on Express's request and response objects instead of middleware.

This allows us to write lean decoupled controllers that can easily be re-used in different route handlers.

Jump to example use cases

Installation

# Via NPM
npm install express-map-middleware --save

# Via Yarn
yarn add express-map-middleware

Example Use Cases

Setting res.locals template values

The snippet below sets two properties on res.locals:

  • res.locals.pageTitle
  • res.locals.pageHeading

By using mapResponse we separate setting template data from our controllers.

const {mapResponse} = require('express-map-middleware');

route.get('/', [
  mapRespone({
    locals: {
      pageTitle: 'Home | My App',
      pageHeading: 'Welcome to my app!'
    }
  })
],
(req, res) => {
  res.render('readAllNotes');
});

Normalizing query strings values

The snippet below normalizes query string values.

By using mapRequest we can separate this logic from our controllers.

const {mapRequest} = require('express-map-middleware');

route.get('/pets?type=dog', [
  mapRequest({
    query: {
      dog: (value, req, res) => {
        if (value === 'dog') {
          return value
        }
        
        return '';
      }
    }
  })
],
(req, res) => {
  res.render('readAllNotes');
});

Configuring request data

In the snippet below we want to re-use our noteController.

By using mapRequest we leverage the pagination logic already in place and simply set the desired query string values on the request object as opposed to modifying the controller code to handle this corner case for the /dashboard route.

const {mapRequest} = require('express-map-middleware');

router.get('/dashboard', [
  mapRequest({
    query: {
      limit: 15,
      since: utilities.normalizeSinceDate
    }
  }),
  noteController.readAll
],
(req, res) => {
  res.render('readAllNotes');
});

API Reference

mapRequest(options)

The key/values in the options object will be evaluated and written to Express's request object.

Each value is evaluated and assigned based on it's type:

Value TypeEvaluation result
ObjectValue assigned to the key
ArrayValue assigned to the key
StringValue assigned to the key
FunctionInvoked and resulting value assigned to the key

Function Value Type Evaluation

Values that are functions will be invoked for each incoming request.

The function is invoked with the following parameters.

exampleFunction(value, request, response)
Function ParameterDescription
valueCurrent value assigned to the key on the request object
requestExpress's request object
responseExpress's response object

Caveats

  • If a key is present on the request object it's value will be overwritten.
  • If you do not want to overwrite a value, assign a function to the key that checks the value argument prior to returning a new value.

mapResponse(options)

The key/values in the options object will be evaluated and written to Express's response object.

Each value is evaluated and assigned based on it's type:

Value TypeEvaluation result
ObjectValue assigned to the key
ArrayValue assigned to the key
StringValue assigned to the key
FunctionInvoked and resulting value assigned to the key

Function Value Type Evaluation

Values that are functions will be invoked for each incoming request.

The function is invoked with the following parameters.

exampleFunction(value, request, response)
Function ParameterDescription
valueCurrent value assigned to the key on the response object
requestExpress's request object
responseExpress's response object

Caveats

  • If a key is present on the response object it's value will be overwritten.
  • If you do not want to overwrite a value, assign a function to the key that checks the value argument prior to returning a new value.

Contributing

Coming soon..

License

MIT (See LICENSE)