ExpressJS: Route Parameter Pre-conditioning
Express JS is a very flexible yet powerful framework on top of exponentially growing Node JS Environment.
Express makes it really easy to handle requests and how the server interacts with the outside world. It is very simple to get going and it has immense amounts of extremely useful features, Middleware is one of those features of express that stand out and are very useful.
Essentially when express has a request it flows through many layers of functions called middleware, which can be used to manipulate those requests on our will.
app.configure(function() {
...
...
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
In the above partial snippet of express code, when a request comes, it flows through the routing functions because of the simple fact that app.use(app.router)
is written before express.static(...)
, if any of those routing functions do not fulfill the request, then that request falls through to the express.static(...)
layer.
Now in order to use these awesome middleware, lets have a look at the following route :-
app.get('range/:min/:max', function(req, res) {
var min = parseInt(req.params.min, 10);
var max = parseInt(req.params.max, 10);
...
// do something with min and max
...
});
The above code is a simple get route, with route parameters namely min
and max
, now what if i have the same route params in many more routing functions, i have to do the same processing with min and max, within each of those functions. Given that the above example is too trivial so lets assume that its a big complex calculation, that equates to redundant code in the routing functions.
So what can express do to help, well lets look at the re-written code below:-
app.param('min', function(req, res, next, min) {
req.min = parseInt(min, 10);
next();
});
app.param('max', function(req, res, next, max) {
req.max = parseInt(max, 10);
next();
});
app.get('range/:min/:max', function(req, res) {
var min = req.min; // It's that
var max = req.max; // simple
...
// do something with min and max
...
});
Now let’s try and digest the above code, firstly param request are executed before the routing functions, thus the above app.param(...)
methods execute before app.get(...)
, now express passes the next
function to every of these functions, which are important as they allow the request to fall through to the next function in the queue for that request. Express also passes the value of the parameter to the function itself, making it even simpler to use. By using this technique we can reduce redundant code from our routing functions.
Okay, so that was one way of doing it. Wait, there’s another way of doing it? Well, yes.
lets say that you have to call a method in some of your routing functions, whose values are used in the routing function itself, generally what we do is call it at the top. But, express provides a better way of doing it.
In express routing calls, we can pass as many functions as we like and they get executed in order, consider the following snippet:-
function doSomeProcessingOnSomething(req, res, next) {
req.operationResult = getResultOfSomeOperation();
next();
});
app.get('getMissileLaunchCodes', doSomeProcessingOnSomething, function(req, res) {
var launchKey = req.operationResult;
...
// do something with those launch codes, Maybe just destroy those missiles :)
...
});
Pretty basic express route, except that now it calls another function before actually doing its work. Pretty Sweet, huh?
well those are some tips every user of express and node must know.
And that brings us to the end. Hope you guys enjoyed it.
Until next time, ciao!
Thank You
Manoj Nama
Intelligrape Software
An attention-grabbing dialogue is worth comment. I feel that you need to write extra on this subject, it might not be a taboo topic but generally people are not sufficient to talk on such topics. To the next. Cheers