這是我第一次使用node.js,我嘗試使用express.io保存cookie。Express.io「無法讀取undefined的'expires'屬性
我從字面上抄送教程(https://github.com/techpines/express.io/tree/master/examples#sessions),但我只收到此錯誤:
[email protected] [/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安裝express.io
任何幫助,非常感謝!
爲什麼'服務器的app.get'部分註釋掉?如果沒有路由,客戶端加載了哪個頁面? –
我更新了我的問題,我嘗試過將app.get註釋掉並添加它。我假設*節點服務器不應該在public_html文件夾中,因爲人們可以訪問腳本。我對node.js仍然是全新的,並且正在努力學習它是如何工作的。我認爲我做錯了很直接的事情。 – Corey