Authored by 毕凯

增加session和config

... ... @@ -10,9 +10,18 @@ var express = require('express'),
var logger = require('morgan'),
favicon = require('serve-favicon'),
cookieParser = require('cookie-parser');
cookieParser = require('cookie-parser'),
session = require('express-session'),
connectRedis = require('connect-redis');
var app = express();
var config = require('./config');
var app = express(),
RedisStore = connectRedis(session);
if (app.get('env') === 'development') {
config = Object.assign(config, require('./config.dev'));
}
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
... ... @@ -24,7 +33,18 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// session 配置
app.use(session({
name: 'yoho-activity',
resave: true,
saveUninitialized: true,
store: new RedisStore(config.sessionRedis),
secret: config.sessionSecret
}));
app.use(function(req, res, next) {
// console.log(req.session);
res.locals.isDevelop = app.get('env') === 'development';
next();
});
... ...
module.exports = {
sessionRedis: {
host: '127.0.0.1',
port: 6379,
prefix: 'activity-node-session:',
ttl: 3600 // session 默认有效期
},
sessionSecret: '111'
};
... ...
module.exports = {
sessionRedis: {
host: '127.0.0.1',
port: 6379,
prefix: 'activity-node-session:',
ttl: 3600 // session 默认有效期
},
sessionSecret: '111'
};
... ...
... ... @@ -9,9 +9,11 @@
},
"dependencies": {
"body-parser": "~1.13.2",
"connect-redis": "^3.0.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"express-session": "^1.13.0",
"hbs": "~3.1.0",
"lodash": "^4.6.1",
"morgan": "~1.6.1",
... ...