I would like to add an optional referral parameter to every URL of my node.js app. I could do the following, but I am thinking about a more efficient/elegant way of achieving this, so my code won't end being too long unnecessarily:
app.get('/product/:example/', function (req, res) {
res.render('product.ejs', { product : product[req.param('example')] });
});
app.get('/product/:example/referrer/:user/', function (req, res) {
// do referral stuff
res.render('product.ejs', { product : product[req.param('example')] });
});
I am thinking about something like this (which obviously doesn't work):
app.get('/product/:example/', function (req, res) { ... });
app.get('/category/:example/', function (req, res) { ... });
// set up all GET requests ...
app.get('/*/referrer/:user/', function (req, res, next) {
// do referral stuff
req.next();
});
Thanks!
Jorge
EDIT: Thank you for your response, mvuajua. I played around with it and I figured out the following. I hope this will help someone:
app.use(function(req, res, next) {
if (req.url.search('/referrer/') > -1) {
res.redirect(req.url.substring(0, req.url.search('/referrer')));
var referrer = req.url.substring(req.url.search('/referrer/')+10,req.url.length);
// do stuff with referrer;
} else {
next();
}
});