middleware-params

1.0.8 • Public • Published

Build Statusnpm version

middleware-params

An easy mechanism for passing data to express middleware

Installation

$ npm install middleware-params

API

const {getData, setData} = require('middleware-params');

The middleware-params module exports two functions.

setData() can be applied as router or application middleware to set values that can be used by later middleware in the chain.

getData() is a convenience function for getting the set data from the request object.


mwParams.setData(data, [options])

Sets data in the request object that can be read from middleware further down the chain.

data

The data to be set. This call will create or replace any data using the the same name (see the options object).

options

An optional options object. Populate the options object if you want to control the key that holds the data. If not defined, this defaults to

{"name": "__PARAM"}

Using the default options argument, stores the data in

req.locals["__PARAM"]

mwParams.getData([options])

Returns the stored data set in earlier middleware or undefined if none is found.

options

Same as the options object in the setData function. You should either default both or provide the same options object to both setData() and getData().


Examples

Usage in application middleware

const express = require('express');
const {getData, setData} = require('middleware-params');
 
const app = express();
let myData = {"somekey": "someval"};
 
// Set some data that will available for all subsequent middleware
app.use(setData(myData));
 
// Define more application middleware
app.use((req, res) => {
  // Use the convenience function to get the data.
  console.log(getData());
  // Get the data manually from the request object
  console.log(req.locals["__PARAM"]);
});
 
// Get the data from a route
app.get("/", (req, res) => {
  // Use the convenience function to get the data.
    console.log(getData());    
});

Usage in routes

const express = require('express');
const {getData, setData} = require('middleware-params');
 
const app = express();
let myData = {"somekey": "someval"};
 
// Set the data in middleware of a route
app.get("/", [setData(myData)], (req, res) => {
  // Use the convenience function to get the data.
    console.log(getData());    
});

Using custom key names

const express = require('express');
const {getData, setData} = require('middleware-params');
 
const app = express();
let myData = {"somekey": "someval"};
 
// Set the data in middleware of a route
app.get("/", [setData(myData, {"name": "custom"})], (req, res) => {
  // Use the convenience function to get the data.
  console.log(getData({"name": "custom"}));
  // This would get the same results
  console.log(req.locals.custom);      
});

Test cases and code coverage

npm test
npm run coverage

License

MIT

Package Sidebar

Install

npm i middleware-params

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

9.11 kB

Total Files

6

Last publish

Collaborators

  • wildbillh