在Cosmos DB(Mongo DB)中存储快速会话的替代方法

时间:2020-10-17 08:47:03

标签: node.js mongodb azure-cosmosdb express-session

我正在使用Node,Mongo和Express JS进行后端REST API。我使用express-session库和connect-mongodb-session来管理会话。

const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

// session management
const store = new MongoDBStore({
    uri: process.env.MONGO_LOCAL_CONN_URL,
    collection: 'sessions'
});



app.use(
    session({
        secret: process.env.SESSION_SECRET,
        resave: false,
        saveUninitialized: false,
        store: store
    })
);

现在,我知道必须在使用Cosmos DB的Azure中部署我的API。根据他们的documentation,我知道Azure Cosmos DB does not yet support server side sessions commands.

我更改了代码,以如下方式在文件模式下存储会话

const session = require('express-session');
var FileStore = require('session-file-store')(session);

app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: false,
    store: new FileStore,
    cookie: { maxAge: 3600000, secure: false, httpOnly: true }
}));

我的问题是,是否可以使用DB而不是File Mode存储会话?另外,当我使用文件模式时,经常收到以下警告消息。

[session-file-store] will retry, error on last attempt: Error: ENOENT: no such file or directory, open 'sessions/G7hK-eOMIbCnk-LhdlV5ihgzFgRwnxZR.json'

是否有最好的方法来处理与Azure Cosmos DB的会话?

0 个答案:

没有答案