Last Updated: February 25, 2016
·
782
· d1b1

Node.js ExpressJS App Must Have

/*
This is a super simple way to get at URL query values without having
to require and parse each time a value is needed. This will run every
time the ExpressJS server gets a request. It parses and places the results
into the req variable. as urlParams.

This will DRY out a route call without changing the expected behavior.

*/

app.use(function(req, res, next) {
var url = require('url');
var queryURL = url.parse(req.url, true);
req.urlparams = queryURL.query;
next();
});

/*
Usage:

var myID = req.urlparams.myID;

*/