externalizing route.param() calls in Express.js

I want to extract some repetitive code into a single module in Node.js and Express.js

Together.js

var express = require('express');
var boom = require('express-boom');
var app = express();
var app.use(boom());    

app.param('user', function(request, reply, next, id){
    request.db.users.get(id, function(err, userInfo){
        if (err) reply.boom.badImplementation(err);
        else if (!userInfo || !userInfo.length) reply.boom.notFound();
        else {
            request.user = userInfo[0];
            next();
        }
    })
})

app.get('/api/users/:user', function(request, reply){
    reply.json(request.user);
});

app.listen(3000);

I have multiple routes I want to use this param conversion including: /users/:user, /api/users/:user, /checkout/:user/:barcode, etc. but each of the root routes (i.e. users, api, checkout) are in their own file and I am attaching them with app.use('/users', userRoutes);. As it is, I will have to put my user param conversion into EACH of these sub-route modules.

I would like to have an interceptors.js where I make all of the common param interceptor functions and only write them once. Here is an example of how I thought it would work.

app.js

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

app.use(require('./routes/interceptors'))

app.use('/users', require('./routes/users'));
app.use('/api', require('./routes/api'));
app.use('/checkouts', require('./routes/checkouts'));

app.listen(3000);

./routes/api.js

var express = require('express');
var api = express.Router();

api.get('/users/:user', function(request, reply){
    reply.json(request.user);
});

module.exports = api;

./routes/interceptors.js

var express = require('express');
var boom = require('express-boom');
var interceptors = express.Router();

var interceptors.use(boom());    

interceptors.param('user', function(request, reply, next, id){
    request.db.users.get(id, function(err, userInfo){
        if (err) reply.boom.badImplementation(err);
        else if (!userInfo || !userInfo.length) reply.boom.notFound();
        else {
            request.user = userInfo[0];
            next();
        }
    })
})

module.exports = interceptors;

There would of course be another file for each of checkout.js and users.js and they will be the same principal as api.js

When I do the above, the param interceptor is never run. No errors are throw that I can see.

Thank you all for any help you may provide, Rhett Lowe

This can't be done.

Param callback functions are local to the router on which they are defined. They are not inherited by mounted apps or routers. Hence, param callbacks defined on app will be trigerred only by route parameters defined on app routes.

http://expressjs.com/api.html#app.param

Another approach you could do is to have a module with your interceptors and require it in your route files where necessary.

./routes/api.js

var express = require('express');
var api = express.Router();
var interceptors = require('./interceptors');

api.use('user', interceptors.user);

api.get('/users/:user', function(request, reply){
  reply.json(request.user);
});

module.exports = api;

./routes/interceptors.js

exports.user = function(request, reply, next, id){
    request.db.users.get(id, function(err, userInfo){
        if (err) reply.boom.badImplementation(err);
        else if (!userInfo || !userInfo.length) reply.boom.notFound();
        else {
            request.user = userInfo[0];
            next();
        }
    })
})

module.exports = interceptors;