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);
});
}));