Skip to content

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

License

Notifications You must be signed in to change notification settings

Jokero/express-params-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published