Merge branch 'hotfix/fix-session-cookie' into 'master'
修复当memcached不可用时,会有一个cookie对象undefined的错误 修复当memcached不可用时,会有一个cookie对象undefined的错误 ![image](/uploads/09aca0cb0ca9ee700699c43ba38a2d8e/image.png) See merge request !107
Showing
1 changed file
with
14 additions
and
9 deletions
@@ -76,6 +76,13 @@ app.use(bodyParser.urlencoded({ | @@ -76,6 +76,13 @@ app.use(bodyParser.urlencoded({ | ||
76 | app.use(cookieParser()); | 76 | app.use(cookieParser()); |
77 | app.use(compression()); | 77 | app.use(compression()); |
78 | 78 | ||
79 | +const sessionStore = new MemcachedStore({ | ||
80 | + hosts: config.memcache.session, | ||
81 | + prefix: 'yohobuy_session:', | ||
82 | + reconnect: 5000, | ||
83 | + timeout: 1000, | ||
84 | + retries: 0 | ||
85 | +}); | ||
79 | app.use(memcachedSession({ | 86 | app.use(memcachedSession({ |
80 | proxy: true, | 87 | proxy: true, |
81 | resave: false, | 88 | resave: false, |
@@ -87,20 +94,14 @@ app.use(memcachedSession({ | @@ -87,20 +94,14 @@ app.use(memcachedSession({ | ||
87 | domain: 'yohobuy.com', | 94 | domain: 'yohobuy.com', |
88 | httpOnly: false | 95 | httpOnly: false |
89 | }, | 96 | }, |
90 | - store: new MemcachedStore({ | ||
91 | - hosts: config.memcache.session, | ||
92 | - prefix: 'yohobuy_session:', | ||
93 | - reconnect: 5000, | ||
94 | - timeout: 1000, | ||
95 | - retries: 0 | ||
96 | - }) | 97 | + store: sessionStore |
97 | })); | 98 | })); |
98 | 99 | ||
99 | app.use(cookieSession({ | 100 | app.use(cookieSession({ |
100 | requestKey: 'session2', | 101 | requestKey: 'session2', |
101 | cookieName: 'yohobuy_session_cookie', | 102 | cookieName: 'yohobuy_session_cookie', |
102 | secret: '82dd7e724f2c6870472c89dfa43cf48d', | 103 | secret: '82dd7e724f2c6870472c89dfa43cf48d', |
103 | - domain: config.cookieDomain | 104 | + domain: 'yohobuy.com' |
104 | })); | 105 | })); |
105 | 106 | ||
106 | app.use((req, res, next) => { | 107 | app.use((req, res, next) => { |
@@ -118,6 +119,10 @@ app.use((req, res, next) => { | @@ -118,6 +119,10 @@ app.use((req, res, next) => { | ||
118 | req.session2.sessionBack = req.session; | 119 | req.session2.sessionBack = req.session; |
119 | } else { | 120 | } else { |
120 | req.session = new memcachedSession.Session(req); | 121 | req.session = new memcachedSession.Session(req); |
122 | + req.session.cookie = new memcachedSession.Cookie({ | ||
123 | + domain: 'yohobuy.com', | ||
124 | + httpOnly: false | ||
125 | + }); | ||
121 | req.session = _.assign(req.session, req.session2.sessionBack); | 126 | req.session = _.assign(req.session, req.session2.sessionBack); |
122 | } | 127 | } |
123 | 128 | ||
@@ -177,6 +182,6 @@ try { | @@ -177,6 +182,6 @@ try { | ||
177 | } | 182 | } |
178 | 183 | ||
179 | // listener | 184 | // listener |
180 | -app.listen(config.port, function() { | 185 | +app.listen(config.port, function () { |
181 | logger.info('yohobuy start'); | 186 | logger.info('yohobuy start'); |
182 | }); | 187 | }); |
-
Please register or login to post a comment