0

I am creating routes in express js from json file with following structure

{
  "/home":{
    "token":"ksdjfglkas"
  },
  "/logout":{
    "token":"ksdjfglksaudhf"
  }
}

I need to be able to access the token inside the routes function. The js that i am using for generating the route is

for(var endpoint in context){
  var route = context[endpoint];
  app.use(endpoint,
    function(req,res,next){
      req.token= route.token;
      next();
    },
    require('./route-mixin'));
}

The problem that i am facing is that route-mixin method always gets the last token.context in this case is just the js file i added above. How can i pass different tokens for each route individually.

georoot
  • 3,557
  • 1
  • 30
  • 59
  • How does your route-mixin file look like? – Michael Troger Sep 14 '16 at 20:49
  • function(req,res,nxt){console.log(req.token);res.send("test");} – georoot Sep 14 '16 at 20:52
  • @MichaelTroger i already tested without the middleware and it works all fine. but console.log is always showing the last token – georoot Sep 14 '16 at 20:53
  • Not 100% sure, but I think with require you include the same instance of this object several times. See https://stackoverflow.com/questions/8887318/understanding-node-js-modules-multiple-requires-return-the-same-object So I would try to inline this function code – Michael Troger Sep 14 '16 at 20:59
  • @MichaelTroger pretty sure that is not it because i even tried console.log in middleware in same file and that even gave the same response – georoot Sep 14 '16 at 21:00
  • That's the only thing which came to my mind: app.use(endpoint, function(req,res,next){ req.token= route.token; next(); }, function(req,res,nxt){ console.log(req.token); res.send("test"‌​); } ); I have no other idea, sry! – Michael Troger Sep 14 '16 at 21:04
  • I got it working! I'll put it as an answer in the next minutes. – Michael Troger Sep 14 '16 at 21:35

1 Answers1

1

The solution to this problem is to put the content within the loop into a closure.

What gave me the idea what's the issue in the first place, was the PhpStorm IDE: mutable variable is accessible from closure

The error message mutable variable is accessible from closure appeared within the first middleware. This article Mutable variable is accessible from closure. How can I fix this? gave me then the hint to use a closure.

So all what was necessary to get it running was changing:

   for(var endpoint in context){
         var route = context[endpoint];
         app.use(endpoint,
             function (req, res, next) {
                 req.token = route.token;
                 next();
             },
             function (req, res) {
                 console.log(req.token);
                 res.send('test');
             }
         );
    }

to:

for(var endpoint in context){
    (function() {
        var route = context[endpoint];
        app.use(endpoint,
            function (req, res, next) {
                req.token = route.token;
                next();
            },
            function (req, res) {
                console.log(req.token);
                res.send('test');
            }
        );
    })();
}

The full example code I was successfully running:

var express = require('express');
var app = express();

var context =  {
    "/home":{
        "token":"ksdjfglkas"
    },
    "/logout":{
        "token":"ksdjfglksaudhf"
    }
};
for(var endpoint in context){
    (function() {
        var route = context[endpoint];
        app.use(endpoint,
            function (req, res, next) {
                req.token = route.token;
                next();
            },
            function (req, res) {
                console.log(req.token);
                res.send('test');
            }
        );
    })();
}

app.listen(3000);
Michael Troger
  • 3,336
  • 2
  • 25
  • 41