这是我第一次使用 node.js,我正在尝试使用 express.io 来保存 cookie。
我从字面上复制了教程(https://github.com/techpines/express.io/tree/master/examples#sessions),但我只收到此错误:
root@server1 [/nodeexample]# node server.js
//Result after First Page Load
{ touch: [Function],
resetMaxAge: [Function],
save: [Function],
reload: [Function],
destroy: [Function],
regenerate: [Function],
name: 'test',
feelings: 'test' }
//Result after refresh
/nodeexample/node_modules/express.io/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:46
expires = 'string' == typeof sess.cookie.expires
^
TypeError: Cannot read property 'expires' of undefined
at /nodeexample/node_modules/express.io/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:46:47
at process._tickCallback (node.js:415:13)
编辑:我在/nodeexample中有主服务器文件,在/home/mysite/public_html/nodetest中有客户端脚本
我确实注意到app.get似乎没有被调用。最后的警报回来了另外,你加入了未定义的
该错误直到第二次刷新才出现,并且看起来好像它最初确实有效。
我的代码是:
服务器:
express = require('express.io')
app = express().http().io()
// Setup your sessions, just like normal.
app.use(express.cookieParser())
app.use(express.session({secret: 'monkey'}))
// Session is automatically setup on initial request.
app.get('/', function(req, res) {
req.session.loginDate = new Date().toString()
res.sendfile('/home/mysite/public_html/nodetest/client.html')
})
// Setup a route for the ready event, and add session data.
app.io.route('ready', function(req) {
req.session.name = req.data
req.session.save(function() {
req.io.emit('get-feelings')
})
})
// Send back the session data.
app.io.route('send-feelings', function(req) {
req.session.feelings = req.data
req.session.save(function() {
req.io.emit('session', req.session)
})
console.log(req.session);
})
app.listen(7076)
客户:
<script src="http://localhost:7076/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:7076');
// Emit ready event.
socket.emit('ready', prompt('What is your name?'))
// Listen for get-feelings event.
socket.on('get-feelings', function () {
socket.emit('send-feelings', prompt('How do you feel?'));
})
// Listen for session event.
socket.on('session', function(data) {
message = 'Hey ' + data.name + '!\n\n'
message += 'Server says you feel '+ data.feelings + '\n'
message += 'I know these things because sessions work!\n\n'
message += 'Also, you joined ' + data.loginDate + '\n'
alert(message)
})
</script>
我不确定我是否缺少依赖项?我用npm install express.io加载它
任何帮助是极大的赞赏!