...
|
...
|
@@ -20,15 +20,10 @@ const path = require('path'); |
|
|
const compression = require('compression');
|
|
|
const bodyParser = require('body-parser');
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
const uuid = require('uuid');
|
|
|
const moment = require('moment');
|
|
|
|
|
|
const pkg = require('./package.json');
|
|
|
const cookieSession = require('client-sessions');
|
|
|
const memcachedSession = require('yoho-express-session');
|
|
|
const memcached = require('connect-memcached');
|
|
|
const hbsEvent = require('./config/hbsevent');
|
|
|
const MemcachedStore = memcached(memcachedSession);
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
const app = express();
|
...
|
...
|
@@ -107,67 +102,7 @@ app.use(bodyParser.urlencoded({ |
|
|
app.use(cookieParser());
|
|
|
app.use(compression());
|
|
|
|
|
|
const sessionStore = new MemcachedStore({
|
|
|
hosts: config.memcache.session,
|
|
|
prefix: 'yohobuy_session:',
|
|
|
reconnect: 5000,
|
|
|
timeout: 1000,
|
|
|
retries: 0
|
|
|
});
|
|
|
|
|
|
app.use(memcachedSession({
|
|
|
proxy: true,
|
|
|
resave: false,
|
|
|
saveUninitialized: true,
|
|
|
unset: 'destroy',
|
|
|
secret: '82dd7e724f2c6870472c89dfa43cf48d',
|
|
|
name: 'yohobuy_session',
|
|
|
cookie: {
|
|
|
domain: 'yohobuy.com',
|
|
|
httpOnly: false
|
|
|
},
|
|
|
store: sessionStore
|
|
|
}));
|
|
|
|
|
|
app.use(cookieSession({
|
|
|
requestKey: 'session2',
|
|
|
cookieName: 'yohobuy_session_cookie',
|
|
|
secret: '82dd7e724f2c6870472c89dfa43cf48d',
|
|
|
cookie: {
|
|
|
domain: 'yohobuy.com',
|
|
|
ephemeral: true
|
|
|
}
|
|
|
}));
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
if (req.session) {
|
|
|
let sessionKeys = Object.keys(req.session || {});
|
|
|
let backSessionKeys = Object.keys(req.session2.sessionBack || {});
|
|
|
|
|
|
if (backSessionKeys.length > sessionKeys.length) {
|
|
|
let differences = _.difference(backSessionKeys, sessionKeys);
|
|
|
|
|
|
_.forEach(differences, d => {
|
|
|
req.session[d] = req.session2.sessionBack[d];
|
|
|
});
|
|
|
}
|
|
|
req.session2.sessionBack = req.session;
|
|
|
} else {
|
|
|
req.session = new memcachedSession.Session(req);
|
|
|
req.session.cookie = new memcachedSession.Cookie({
|
|
|
domain: 'yohobuy.com',
|
|
|
httpOnly: false
|
|
|
});
|
|
|
req.session = _.assign(req.session, req.session2.sessionBack);
|
|
|
}
|
|
|
|
|
|
if (!req.sessionID) {
|
|
|
req.sessionID = uuid.v4();
|
|
|
}
|
|
|
|
|
|
next();
|
|
|
});
|
|
|
|
|
|
require('./doraemon/middleware/yoho-session')(app);
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
req.user = {}; // 全局的用户数据
|
...
|
...
|
|