Skip to content

Instantly share code, notes, and snippets.

@typesafe
Created May 12, 2014 18:47
Show Gist options
  • Save typesafe/017b50bb2cd4b37396da to your computer and use it in GitHub Desktop.
Save typesafe/017b50bb2cd4b37396da to your computer and use it in GitHub Desktop.
resolve parameters in expressjs
var server = require('./server.js');
var collection = require('./collection.js');
var app = server();
app.put('/items/:id/part', server.resolve(collection), function(req, res){
// no more 404 checking
// access resolved item
var item = req.resolved;
item.part = req.body;
// ...
});
var express = require('express');
express.resolve = function(collection, param){
return function(req, res, next){
collection.get(req.params[param || 'id']).then(function (item) {
if(!item) {
res.send(404);
return;
}
req.resolved = item;
next();
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment