如何在 app.js 中获取主机以用于确定环境 (production/development)

How to get host in app.js for use in determining environment (production/development)

我正在尝试获取应用程序当前所在的主机,然后相应地更改一个变量。我知道我可以使用 req.get('host') 获取主机,但我相信我的问题源于回调

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

我想说明一下,我已经阅读了有关异步性的内容并理解为什么 console.log 打印 undefined。我只是不知道如何将回调与 reqres.

联系起来

req.hostreturns一个值正确。我只需要获取当前主机,然后将其用于身份验证目的(生产与开发)

编辑:也许这个额外的代码会帮助其他人理解我想要完成的事情

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

我认为你应该像这样使用 if else;

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

我能够通过将所有身份验证代码移至 app.use 回调中来解决此问题。如果有人有其他解决方案,请告诉我。只是为了访问一个变量的值而将整个代码块粘贴在那里对我来说似乎很奇怪。

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