首頁加載時有多個快速會話實例

[英]Multiple express session instances on first page load


I am automatically giving every user a session and storing the session data in a MongoDB collection called sessions. I am also setting a cookie which allows me to identify every (returning) user. When a user returns, I am successfully able to find my session when the cookie is sent with the request.

我自動為每個用戶提供會話並將會話數據存儲在名為sessions的MongoDB集合中。我還設置了一個cookie,允許我識別每個(返回)用戶。當用戶返回時,我成功地能夠在隨請求發送cookie時找到我的會話。

Everything works fine expect the very first time a user accesses the site. The front-end (which is built with Angular), makes six different calls simultaneously to the Node server (every call has its own purpose). Since there is no cookie set yet, the Node Server thinks there are six different users calling the server by which six sessions are created and stored to the sessions collections.

一旦用戶第一次訪問該站點,一切正常。前端(使用Angular構建)可以同時向節點服務器發出六個不同的呼叫(每個呼叫都有自己的用途)。由於尚未設置cookie,節點服務器認為有六個不同的用戶呼叫服務器,通過該用戶創建六個會話並將其存儲到會話集合中。

The next call (after a user action) happens with the second to last created session and each future call is made with this session id.

下一次調用(在用戶操作之后)發生在倒數第二個創建的會話中,並且每個將來的調用都是使用此會話ID進行的。

How can I solve the initial creation of six different sessions when the user accesses the site for the first time?

當用戶第一次訪問網站時,如何解決六個不同會話的初始創建?

It is possible to perform one call to "initiate" the session and execute the rest after the response. But if there is another option, I would really like to hear it.

可以執行一次調用以“啟動”會話並在響應之后執行其余操作。但如果還有其他選擇,我真的很想聽。

I am using a custom build of Express.js 4.0, Node.js v0.10.32 & MongoDB v2.6.5.

我正在使用Express.js 4.0,Node.js v0.10.32和MongoDB v2.6.5的自定義構建。

This is how I create my session and set the cookie:

這是我創建會話並設置cookie的方法:

app.use(session({
    saveUninitialized: true,
    resave: true,
    cookie: { secure: false, httpOnly: false },
    name: 'someName',
    secret: 'someScret',
    store: new mongoStore({
        db: framework.mongoose.connection.db,
        collection: 'sessions'
    })
}));

If you need more information, please let me know.

如果您需要更多信息,請告訴我。

2 个解决方案

#1


2  

I figured it out myself. There was an issue with OPTIONS requests. Options requests do not send the cookie in their headers which caused the creation of new sessions on the Node server.

我自己弄清楚了。 OPTIONS請求存在問題。選項請求不會在其標頭中發送cookie,導致在節點服務器上創建新會話。

Since options requests are only used for checking for authorization for cross-domains, it must have been something with cors. I disabled cors and started Chrome with cors disabled. This solved the problem.

由於選項請求僅用於檢查跨域的授權,因此它必須與cors有關。我禁用了角色並啟動了Chrome,禁用了cors。這解決了這個問題。

Please note: I only implemented cors for (easier) local development. I do not require cors on the live server.

請注意:我只為(更容易)本地開發實施了角色。我不需要在實時服務器上使用cors。

This was the commando (Mac only) to open Google Chrome:

這是打開谷歌瀏覽器的突擊隊員(僅限Mac):

open -a Google\ Chrome --args --disable-web-security

#2


0  

var session = require('express-session');
    app.use(session({
            secret: 'a4f8071f-c873-4447-8ee2',
            cookie: { maxAge: 2628000000 },
            store: new (require('express-sessions'))({
                storage: 'mongodb',
                instance: mongoose, // optional
                //host: 'localhost', // optional
                //port: 27017, // optional
                //db: 'test', // optional
                collection: 'sessions', // optional
                expire: 86400 // optional
            })
        }));

use express-session and express-sessions

使用快遞會話和快遞會話


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/02/24/72522e1ca18e01aabbaffd68241e6eec.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com