0

I'm trying to get the host the app is currently on and then change a variable accordingly. I know I can use req.get('host') to get the host but I believe my problem stems from callbacks

var callBackUrl;

app.use(function(req,res,next){
    if(req.get('host') == 'localhost:3000'){
        callBackUrl = 'http://localhost:3000/handleauth'; 
    }
    else{
        callBackUrl = 'http://example.com/handleauth';
    }
    console.log('CALL BACK URL: ', callBackUrl); 
    next();
});

console.log(callBackUrl); //undefined

app.use('/', routes);
... //more code

I would like to make a note that I have read about asynchronicity and understand why console.log prints undefined. I simply do not know how to tie callbacks in with req and res.

req.host returns a value correctly. I simply need to get the current host and then use it for authentication purposes (production vs. development)

EDIT: Perhaps this additional code will help others in understanding what I am trying to accomplish

//... original code from question

passport.use(new InstagramStrategy({
    clientID: INSTAGRAM_CLIENT_ID,
    clientSecret: INSTAGRAM_CLIENT_SECRET,
    callbackURL: callBackUrl //set to undefined and therefore authentication fails 
},
function(accessToken, refreshToken, profile, done){
    process.nextTick(function(){
        app.set('instaID', profile.id.toString());
        app.set('fullName', profile.displayName);
        app.set('imgSource', profile._json.data.profile_picture);

        return done(null,profile.id);
    });
}));
prcbass
  • 339
  • 1
  • 3
  • 17
  • 2
    So the real issue is that your middleware is not executed? `console.log(callBackUrl);` will certainly never work where you have it. – Felix Kling Jul 28 '15 at 01:28
  • I use `app.use` for other things and it definitely works. I don't believes the issue lies there. Perhaps I am wrong though – prcbass Jul 28 '15 at 01:29
  • Then what is with the comment `//never gets called`? If you really want to access `callBackUrl` outside the function like shown here, then it's a duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/218196) – Felix Kling Jul 28 '15 at 01:30
  • Well `console.log('CALL BACK URL: ', callBackUrl);` never appears in the output. Not even "CALL BACK URL: undefined" – prcbass Jul 28 '15 at 01:31
  • That seems to suggest that the function passed to `app.use` is never executed, so I would focus on that issue first. Again, you cannot access `callBackUrl` outside the function because at the moment you are trying to do so, the function hasn't run yet. – Felix Kling Jul 28 '15 at 01:33
  • I understand that I can't access callBackUrl outside of the function. I already read the Asynchronous code reference post. I will update my question with an example of app.use working – prcbass Jul 28 '15 at 01:35
  • The whole point of my first comment was to clarify exactly that. – Felix Kling Jul 28 '15 at 01:36
  • Ok, how should I modify my question then? – prcbass Jul 28 '15 at 01:36
  • Depends. I currently see two problems: Why the function is not executed and how to pass information extracted along to other parts of the code. I'd modify the question to focus on one of the issues. – Felix Kling Jul 28 '15 at 01:38
  • I moved the code in question before `app.use("/", routes);` and it now prints to console. That eliminates that problem – prcbass Jul 28 '15 at 01:43
  • Ok, then you probably want to provide more information about where/how you want access the data. – Felix Kling Jul 28 '15 at 01:48
  • Thanks for the input. Added a little bit more info but I believe most of the problem can be seen in the code – prcbass Jul 28 '15 at 01:56
  • @prcbass - why not just set the host as an environment variable or set in config. `HOST=http://example.com/handleauth`. Details of using a config file - https://gist.github.com/swarajgiri/4ac4d63fcf7dfc6fb0b8 – Swaraj Giri Jul 28 '15 at 03:44
  • I suppose that could work but I don't really have separate files for production and development (right now the app on the server is the same as the app on localhost). I'd prefer to just set one variable and determine where I am (localhost or server) from there – prcbass Jul 28 '15 at 03:59
  • @prcbass - in that case, just have it as an environment variable `HOST=http://localhost:3000/ node server.js`. – Swaraj Giri Jul 28 '15 at 06:43

2 Answers2

0

I think you should use if else like this;

var callBackUrl;

app.use(function(req,res,next){
    if(req.get('host') == 'localhost:3000'){
         callBackUrl = 'http://localhost:3000/handleauth';
         console.log('CALL BACK URL: ', callBackUrl); 
         next(); 
    }
    else{
         callBackUrl = 'http://example.com/handleauth';
         console.log('CALL BACK URL: ', callBackUrl); 
         next();
     }
 });

console.log(callBackUrl); //undefined

app.use('/', routes);
... //more code
EdisonGuo
  • 11
  • 3
  • That does not fix my problem. I am not trying to pass the host data to the next middleware, I simply want to use it right after setting it inside the `app.use()` callback. I have edited my question once again with another example of what I need the url for – prcbass Jul 28 '15 at 06:16
0

I was able to fix this problem by moving all the authentication code into the app.use callback. If anyone has another solution please let me know. It just seems strange to me to stick that entire block of code in there just to access the value of one variable.

var callBackUrl;

app.use(function(req,res, next){
    if(req.get('host') == 'localhost:3000'){
        callBackUrl = 'http://localhost:3000/handleauth'; 
    }
    else{
        callBackUrl = 'http://example.com/handleauth';
    }
    console.log('CALL BACK URL: ', callBackUrl);

    passport.serializeUser(function(user,done){
        done(null,user);
    });

    passport.deserializeUser(function(obj,done){
        done(null,obj);
    });

    passport.use(new InstagramStrategy({
        clientID: INSTAGRAM_CLIENT_ID,
        clientSecret: INSTAGRAM_CLIENT_SECRET,
        callbackURL: callBackUrl //no longer undefined
    },
    function(accessToken, refreshToken, profile, done){
        process.nextTick(function(){
            app.set('instaID', profile.id.toString());
            app.set('fullName', profile.displayName);
            app.set('imgSource', profile._json.data.profile_picture);

            return done(null,profile.id);
        });
    }));

    next();
});

console.log("URL: ", callBackUrl) //still prints undefined but that it ok since we use the value of callBackUrl inside the callback

app.use('/', routes);
prcbass
  • 339
  • 1
  • 3
  • 17