我刚进入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中的错误:浏览器中无错误:无,但无。我的意思是没有回应。一直等着
这里出了什么问题。任何帮助都将不胜感激。谢谢
发布于 2017-05-05 16:29:19
我通过直接将"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);发布于 2017-05-05 13:27:48
我在一个示例应用程序上复制了您的场景,并在"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'));它的工作绝对是找到了。有关更多细节,您可以共享错误日志吗?
发布于 2017-05-05 13:57:20
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
}));这就是我通常在特快专递上做这件事的方式。
https://stackoverflow.com/questions/43804722
复制相似问题