azat-co / expressworks

Learn Express.js from the author of one of the best books on Express.js—Pro Express.js— with this workshop that will teach you basics of Express.js.
MIT License
715 stars 220 forks source link

param_pam_pam could've used app.param #72

Open grabbou opened 9 years ago

grabbou commented 9 years ago

It'd be quite cool to show users how they can profit from app.param.

app.param('message_id', function(req, res, next, messageId) {
    req.computedHash = require('crypto')
        .createHash('sha1')
        .update(new Date().toDateString() + messageId)
        .digest('hex')
    next();
});

app.get('/:message_id', function(req, res, next) {
    res.send(req.computedHash);
});

Maybe worth mentioning there are two ways to approach this problem? Showing the beginners not everything needs to be handled in a route handler might be a nice tip!

WDYT?

grabbou commented 9 years ago

(But actually with Express being so unopinionated they can even use general-purpose middleware so mind-fuck guaranteed)

azat-co commented 9 years ago

@grabbou feel free to make a pull request. It's a good idea to show param and general middleware so maybe create both.