我有以下代码来验证用户是否登录到我的应用程序。
我的问题是,用户登录后,img 请求的 session 与我为用户保存的 session 不同。
我有以下代码来验证用户是否登录到我的应用程序。
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
globalApp.mongo.getUserBySessionId(req.sessionID,function(user){
console.log("SessionID :"+req.sessionID);
console.log("User : "+ JSON.stringify(user));
console.log("Route : "+JSON.stringify(req.route.params));
if(user){
req.user = user;
res.locals.user = req.user;
res.locals.security = globalApp.security;
}else{
if(publicPaths.indexOf(req.route.params[0])==-1){
res.send({error:true,reason:'You are not logged in, please re-start the app.'});
return;
}
}
next();
});
});
我请求这样的图像:
<img src="/images/loading.gif" width="49" height="49"/>
有趣的是,所有其他类型的文件都得到了正确的服务。服务器正在为每个 img 请求创建一个新 session 。
此外,只有当我的应用程序在 appfog 上运行时才会发生这种情况。在我的计算机(本地主机)中它工作正常。
有什么建议吗?
非常感谢。
最佳答案
我知道我迟到了,但我也遇到了同样的问题。看来 AppFog 会剪切图像的 cookie,并且可能是其他一些资源,因此没有简单的方法来控制 session 的图像访问。例如,您可以将资源“/images/loading.gif”重命名为“/images/loading”。 http://feedback.appfog.com/forums/171983-appfog/suggestions/4299861-do-not-cut-cookie-for-images-and-other-resources
关于node.js - nodejs + express + appfog img 资源的不同 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177570/