在 Node.js 中设置 mssql-session-store



我正在尝试使用 mssql-session-store 作为会话的 nodejs(express( 存储:https://www.npmjs.com/package/mssql-session-store

这是应该如何配置它(从 npm 页面(:

app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: new MssqlStore(options) // see options below
}));
var options = {
    connection: existingConnection,
    ttl: 3600,
    reapInterval: 3600,
    reapCallback: function() { console.log('expired sessions were removed); 
   }
};

我的问题是选项连接。它应该是"来自 mssql 的连接的可选实例"。此连接建立是一个异步过程(从 npm 页面(:

const sql = require('mssql')
async () => {
try {
    await sql.connect('mssql://username:password@localhost/database')
    const result = await sql.query`select * from mytable where id = 
${value}`
    console.dir(result)
 } catch (err) {
    // ... error checks
 }
}

这就是在 nodejs 初始化中定义快速会话的方式:

app.use(session({
name:<session name>,
key: <session key id>,
resave:false, 
saveUninitialized:false, 
secure: process.env.NODE_ENV ==="production",  
secret:<secret string>, 
store: new MssqlStore(options), //This is optional - for use when using sql server as a store
cookie:{
        //httpOnly: true, 
        secure: process.env.NODE_ENV ==="production", 
        expires: config.expressSession.cookieLifeTime 
    }
 }));

问题是建立连接是一个异步过程。我已经尝试了几个版本来在应用程序中使用快速会话,但是在设置连接(异步(后才这样做。请参阅我的基本代码(节点初始化.js - servre.js 文件(:

const express = require('express');
const app = express();
const sql = require('mssql');
const session = require ('express-session'); 
const MssqlStore = require ('mssql-session-store')(session);
var sqlStore = null;
var store = null; 
var mssqlConfig = 
{
  user: <user>
  password: <password>,
  server: <server name>
  database: <database>,     
  options: {
   encrypt: true // Use this if you're on Windows Azure
  }
}

我尝试在连接承诺的应用程序中设置会话:

var sqlConnection = null;
async function getConnectedConnectionOptions()
{
  try 
  {
      sqlConnection = await sql.connect(<connection string>);
      return await Promise.resolve(sqlconnection: sqlConnection);
  } catch (err) 
  {
      sqlConnection = null;
  }
}
getConnectedConnectionOptions(),then(result => 
     app.use(session({
                    name:<session name>,
                    key: <session key id>,
                    resave:false, 
                    saveUninitialized:false, 
                    secure: process.env.NODE_ENV ==="production",  
                    secret:<secret string>, 
                    store: new MssqlStore(result) , 
                    cookie:{ 
                      //httpOnly: true, 
                        secure: process.env.NODE_ENV ==="production", 
                        expires: config.expressSession.cookieLifeTime 
            }
        }));

但是,还有一个范围问题,即会话未在全局应用程序中定义。请支持。

这是 mssql-session-store 模块中的示例文件夹内

var dbConfig = {
  server: "localhost\sqlexpress",
  database: "sessiontest",
  user: "sa",
  password: "atonan"
};
var start = function(callback) {
  callback = callback || function() {};
  sql.connect(dbConfig, function(err) {
    if (err) return callback(err);
    var app = express();
    app.use(session({
      secret: '991E6B44882C4593A46C0DDFCA23E06A',
      resave: false,
      saveUninitialized: false,
      store: new MssqlStore({ reapInterval: 10, ttl: 10 })
    }));

最新更新