GithubHelp home page GithubHelp logo

jokero / express-params-loader Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 14 KB

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

License: MIT License

JavaScript 100.00%

express-params-loader's Introduction

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

express-params-loader's People

Contributors

jokero avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

express-params-loader's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.