2013-02-25 37 views
0

我使用快遞3.x和連接 - 蒙戈和請求模塊複製的NodeJS cookie進行內部應用程序的請求

我的應用程序有一些中間件,確保外部請求有一個的access_token。 檢查access_token並將一些數據存儲在會話中。 然後,我想在應用程序內部調用一個URL,但內部調用獲得新會話(因爲它是來自用戶瀏覽器請求的單獨請求)。 我想要做的是以某種方式將Express簽名的Cookie複製到內部請求()中,以便中間件根據原始外部會話標識執行操作。 我曾嘗試將Cookie jar傳遞給請求對象,但它似乎並不支持已簽名的cookie。任何想法我可以做到這一點?

/* Middleware to check access tokens */ 
app.all("/*", requireAuth, function(req, res, next) { 
    next(); 
}); 

function requireAuth(req,res,next) { 

if (req.query.access_token && !req.session) { 

    // Check the access token and popualte stuff in the session 
    req.session.somedata = 'test'; 

    // Then call a url internall to do something 

    // My issue is that this INTERNAL request below gets a new session Id 
    // so it itself returns Not Authorised as its hits the same code 

    request({ 
     url: 'someurlinside-this-node-app', 
     method: "GET" 
    }, function _callback(err, serviceres, body) { 
     next(); 
    }); 

}else{ 
    res.send('Not Authorised'); 
} 

} 

回答

0

Cookies是隻是一個標題,所以如果你想通過它一起你應該能夠做到這一點:

var cookies = req.header('Set-Cookie'); 

request({ 
     url: 'someurlinside-this-node-app', 
     method: "GET", 
     headers: { 'Set-Cookie' : cookies} 
    }