express-params-loader
Loader for express app.param() and router.param() methods. Extends req with object loaded from MongoDB or other source
Note: This module will only work with Node.js >= 4.0 and Mongoose >= 4.0.
Installation
npm install express-params-loader
Usage
To load object you can use custom load function or Mongoose model:
loadObject(modelOrLoadFunction, [options])
Parameters
modelOrLoadFunction
{Model | Function} - Mongoose model or custom load function that returns a promise[options]
{Object}[fieldName=_id]
{String} - Field that is used to search for a document (only for model)[objectName]
{String} -req
property for object loading. Default value:lowerCamelCased model name
for model and"object"
for load function[passErrorToNext=true]
{Boolean} - Shouldnext()
function be called with error if object not found?[errorFactory]
{Function} - Factory for error creation if object not found[errorMessage]
{String | Function} - Error message
Examples
Mongoose model
var express = ;var loadObject = ; var app = ; app; // Book is Mongoose model app;
By default object is loaded to req[<lowerCamelCased model name>]
. You can change it using objectName
option:
app; app;
Loader finds a single document by its _id
field. You can use another field with fieldName
option:
app; app;
Load function
app; app;
By default object is loaded to req.object
. But you can change it using objectName
too:
app; app;
Custom default options
config.js:
loadObjectoptions = objectName: 'loadedObject';
app.js:
app; app;
Tests
npm installnpm test