【问题标题】:How can I write a module to handle ExpressJS session?如何编写一个模块来处理 ExpressJS 会话?
【发布时间】:2017-05-06 00:29:19
【问题描述】:

我是 ExpressJS 的新手(才 3 个月),我正在尝试制作一个项目来实践我目前所学的知识。

我尝试编写一个模块来处理快速会话。但它似乎不起作用 - 没有错误也没有响应。

代码是:

var express = require("express");
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);

// MySQL Session Configuration
const mySQLSessionConfiguration = {
    host: 'localhost',
    port: 1234,
    user: 'thisIsNotMyRealUsername',
    password: 'neitherThisIsMyRealPassword',
    database: 'aDatabase'
};

// Create Session
module.exports = function (){
    return (session({
    store: new MySQLStore(mySQLSessionConfiguration),
    secret: 'LOL',
    resave: true,
    saveUninitialized: true
    }));
};

在我的 index.js 文件中:

app.use(require("./modules/session.js"));

// 如果我直接在 index.js 中编写代码,代码可以正常工作,但我想编写一个模块 - 我想学习。

CLI 中的错误:无 浏览器中的错误:没有但没有。我的意思是没有回应。继续等待

这里有什么问题。任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: javascript node.js express express-session


    【解决方案1】:

    我已经在示例应用程序上复制了您的场景,并在“session.js”文件中编写了以下代码

    var clientsession = require('client-sessions');
    
    module.exports = function (){
      return (clientsession({
        cookieName: 'session',
        secret: 'secret',
        duration: 30 * 60 * 1000,
        activeDuration: 5 * 60 * 1000,
        }));
    };
    

    然后在 server.js 中我使用它如下:

    app.use(require('./session.js'));
    

    它的工作绝对找到。 您能否分享您的错误日志以获取更多详细信息?

    【讨论】:

    • 嗨,感谢您的宝贵时间。我已经解决了这个问题。而不是返回会话对象。我已将“app”直接传递到模块中,然后使用“app.use(我返回的所有与会话相关的东西);”
    【解决方案2】:
    var cookieParser = require('cookie-parser');    
    var session = require('express-session');
    var MySQLStore = require('express-mysql-session')(session);
    
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : 'localhost',
      user     : '< MySQL username >',
      password : '< MySQL password >',
      database : '<your database name>'
    });
    
    connection.connect();
    
    app.use(cookieParser());
    app.use(session({
      secret: 'supersecret',
      resave: true,
      saveUninitialized: true,
      store: new mySQLStore(mysqlConnection: mysql.connection),
      cookie: { maxAge: 1000 } //whatever you'd like
    }));
    

    这就是我通常在 express 中使用 express-session 的方式。

    【讨论】:

    • 感谢您的回答。我用同样的方法。但我只是想制作一个模块,以便我可以清除我的概念 - 我是新来表达的。问题已解决。
    【解决方案3】:

    我已通过将“app”直接传递到模块而不是从模块返回会话来解决问题。

    固定代码如下所示:

    // var express = require("express"); -- not required at all
    var session = require('express-session');
    var MySQLStore = require('express-mysql-session')(session);
    
    // MySQL Session Configuration
    const mySQLSessionConfiguration = {
        host: 'localhost',
        port: 1234,
        user: 'thisIsNotMyRealUsername',
        password: 'neitherThisIsMyRealPassword',
        database: 'aDatabase'
    };
    
    // Create Session
    module.exports = function (app){    // app received :D
        app.use(session({               // app.use instead of return
        store: new MySQLStore(mySQLSessionConfiguration),
        secret: 'LOL',
        resave: true,
        saveUninitialized: true
        }));
    };
    

    在 index.js 中

    var alpha = require("./modules/session.js")(app);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2013-08-03
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多