New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

express-params-loader

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-params-loader

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

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 30 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc