epl

express-params-loader

Loader for express app.param() and router.param() methods. Extends req with object loaded from MongoDB or other source

Showing:

Popularity

Downloads/wk

2

GitHub Stars

2

Maintenance

Last Commit

5yrs ago

Contributors

0

Package

Dependencies

1

Size (min+gzip)

1.9KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

express-params-loader

Loader for express app.param() and router.param() methods. Extends req with object loaded from MongoDB or other source

NPM version Build status

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} - Should next() 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    = require('express');
var loadObject = require('express-params-loader');

var app = express();

app.param('id', loadObject(Book)); // Book is Mongoose model

app.get('/books/:id', function(req, res, next) {
  // req.book is loaded book
});

By default object is loaded to req[<lowerCamelCased model name>]. You can change it using objectName option:

app.param('id', loadObject(Book, { objectName: 'loadedBook' }));

app.get('/books/:id', function(req, res, next) {
  // req.loadedBook
});

Loader finds a single document by its _id field. You can use another field with fieldName option:

app.param('title', loadObject(Book, { fieldName: 'title' }));

app.get('/books/by-title/:title', function(req, res) {
    // req.book
});

Load function

app.param('id', loadObject(function(req, id) {
    // load function must return promise
    return Promise.resolve({ id: 1, title: 'The Lord of the Rings' });
}));

app.get('/books/:id', function(req, res, next) {
  // req.object is loaded book
});

By default object is loaded to req.object. But you can change it using objectName too:

app.param('id', loadObject(
    function(req, id) {
        return Promise.resolve({ id: 1, title: 'The Lord of the Rings' });
    },
    { objectName: 'book' }
));

app.get('/books/:id', function(req, res, next) {
  // req.book is loaded book
});

Custom default options

config.js:

loadObject.options = { 
    objectName: 'loadedObject'
};

app.js:

app.param('id', loadObject(Book));

app.get('/books/:id', function(req, res, next) {
  // req.loadedObject
});

Tests

npm install
npm test

License

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100