It wasted me more than 6 hours to Google through the topic without reaching an answer.
As I tried to write some examples in book "Node.js: Up and Running" in LiveScript, I bumped into this weird behaviour.
Chrome v26 / Linux did not store my sessionID in the cookie, or more exactly, it stored but I couldn't retrieve it as I reload the page, resulting in that session is always regenerated.
Interestingly, Firefox works properly, and so do Chrome v22/Linux and Chrome v29/Windows.
I tried to write a small piece in both PHP and LiveScript. Cookie works. So the problem must be Express 3.x.
# For finding the root cause of 7-24 on Chrome 26
require! express
app = express!
app.use express.cookieParser!
app.get '/visit', (req, res) !->
visit = parseInt req.cookies.visit or 0
res.cookie 'visit', visit + 1
res.send 'Visit #' + visit
app.get '/clear', (req, res) !->
res.clearCookie 'visit'
res.send 'Cookie cleared.'
app.listen 8001
I noticed that from Express 2.x to 3.x, the
connect / anonymous
function has been changed from
(req, res)
to
(req, res, next)
. Nice bet, the answer is 42.
Here's the code that works:
app.configure !->
app.use express.cookieParser 'sign'
app.use express.session {
secret: 'secretKey'
store: store
}
app.use (req, res, next) !->
sess = req.session
# console.log "store = "
# console.log store
# console.log "sess.email = " + sess.email
res.render 'socket.jade', {email: sess.email || 'noSessMail'}
console.log "SessionID in Express = " + req.sessionID
next and next! # NOTICE THIS LINE
I don't know how to use
express.cookieSession()
, though. Not many examples are there.
You can find the Git repository to my practice
here.
沒有留言:
張貼留言