Skip to content
Express middleware to automatically map routing parameters onto Mongoose documents
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore
LICENCE.md
README.md
index.js
package.json

README.md

param2doc

Express middleware to automatically map routing parameters onto Mongoose documents

Installation

$ npm install param2doc

Usage

Example

var express = require('express');
var param2doc = require('param2doc');
var app = express();

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var exampleSchema = mongoose.Schema({
  name: String
});

mongoose.model('Example', exampleSchema);

param2doc.init({mongoose: mongoose});

app.get("/path/:example", param2doc(), function (req, res) {
  if (req.documents.example)
    res.send("<h1>" + req.documents.example.name + "</h1>");
  else
    next();
});

When called with the URL http://server/path/0123456789ABCDEF then req.documents.example will contain a referemce to a document of the Example model with the ID 0123456789ABCDEF (if it exists).

TODOs / Future features

  • Support app.params()
  • For more see TODOs in source code
Something went wrong with that request. Please try again.