2

I have some code that is repeated among my routes, and I always pass to the view some resulting variables from that repeated code.

Example:

/* GET /events/list */
router.get('/events/list', function(req, res) {

  var blah = someFunctionThatAnalyzesUserCookie();

  res.render('places/list', { title: 'Places', blah: blah });
});

/* GET /events/details */
router.get('/events/details', function(req, res) {

  var blah = someFunctionThatAnalyzesUserCookie();

  res.render('places/details', { title: 'Some place', blah: blah });
});

How can I refactor this?
I need to pass blah to all of my views and I want their logics clean, like this:

/* GET /events/list */
router.get('/events/list', function(req, res) {

  res.render('places/list', { title: 'Places' });
});

/* GET /events/details */
router.get('/events/details', function(req, res) {

  res.render('places/details', { title: 'Some place' });
});

I googled for a while but only found how to execute something in every request, like this:

app.use(function(req, res, next) {

    doSomethingInEveryRequest();

    next();
});

But I can't figure out how to pass a variable to the view in every request.

Also I read about app.locals, but I guess that is more suited for constants. My passed variable depends on the user's cookies so is a... variable.

3
  • can you try extracting a function with two parameters path, renderFileName & title? Commented Jul 18, 2014 at 4:29
  • hummm I don't understand the idea that you are suggesting. What is renderFileName? Also i should point out that some of these methods pass the title and some pass more data... but they always pass blah Commented Jul 18, 2014 at 4:31
  • I've added an answer, tell me where it's failing. Commented Jul 18, 2014 at 4:44

2 Answers 2

2

Can you try something like bellow:-

var renderPage = function(path,fileName,title){
    router.get(path,function(req,res){
        var blah = someFunctionThatAnalyzesUserCookie();
        res.render(fileName,{title:title, blah:blah});
    });
}

renderPage('/events/list','events/list','Places');
renderPage('/events/details','events/details','Some Place');
Sign up to request clarification or add additional context in comments.

3 Comments

that technique is called middleware?
ohhhh you are asking, No I don't think so, I just re-factored the code to remove duplication.
well I will mark it as accepted because you gave me the main idea of what I have to do. To make it clear for the rest of the world: instead of using res.render(viewPath, obj) at the end of every request, I will be using a custom method, renderPage, which at the end does use res.render(...) but with the opportunity there to alter every response. :p
0

I think you can use Express's middleware called "Cookie-Parser".

Here is a link to its documenation: https://github.com/expressjs/cookie-parser/blob/master/README.md

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.