所以我的目标是在
目前,我可以在主域上设置和获取任何 Cookie,在我的情况下,它是通过
这基本上是我的 Express.js 应用程序的外观:
domain.com
上设置一个可供所有子域名 *.domain.com
使用的 cookie。我目前使用的是 Express 4.x 和 NodeJS。目前,我可以在主域上设置和获取任何 Cookie,在我的情况下,它是通过
lvh.me:3000
在本地环境中进行测试(允许使用本地子域名)。这基本上是我的 Express.js 应用程序的外观:
// Require all files (express,http,express-session,cookie-parser...)
var api = express()
// Cookie Parser
api.use(cookieParser());
api.use(session({
secret: 'yoursecret',
cookie: {
path: '/',
domain: '.lvh.me:3000',
maxAge: 1000 * 60 * 24, // 24 hours
},
resave: true,
saveUninitialized: true
}));
// CORS
api.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next()
})
// View Engine
api.set('views', './api/views');
api.set('view engine', 'ejs');
api.disable('x-powered-by');
// Stylus
function compile(str, path) {
return stylus(str).set('filename', path);
}
api.use(stylus.middleware({
src: './app',
compile: compile
}));
// Serving Static Content
api.use(express.static('./app'));
// All Routes
require('../api/routes/test.server.routes.js')(api);
// Router
api.get('*', function(req, res) {
//res.cookie('remember', 1, { domain : "lvh.me:3000" });
res.render('index'); // Get all requests to Index. Angular takes over routing.
});
// Return Application
return api;
Cookie是通过Cookie解析器设置的res.cookie('user', 'bobby');
,在前往lvh.me:3000/anything
时似乎没有问题,但前往boddy.lvh:3000/anything
时Cookie为空。
我有什么遗漏吗?我以为Cookie将在所有子域中都可用?我已经在Stack Overflow上阅读了一些文章/帖子,但它们似乎都过时了。非常感谢任何指导或帮助。
另外,如果我在Express文件中设置Cookie,它将在整个应用程序中可用。例如:
// Router
api.get('*', function(req, res) {
res.cookie('user', 'jim'); // This can be accessed
res.render('index');
// Get all requests to Index. Angular takes over routing.
});
有什么原因吗? - 这很大程度上是因为它被设置在任何/每个视图上。所以这仍然让我面临最初的问题。