简体   繁体   English

Express.io“无法读取未定义的'expires'的属性”

[英]Express.io “Cannot read property of 'expires' of undefined”

This is my first time working with node.js and I am trying to use express.io to save cookies. 这是我第一次使用node.js,并且尝试使用express.io保存cookie。

I have literally carbon copied the tutorial ( https://github.com/techpines/express.io/tree/master/examples#sessions ) but I only receive this error: 我从字面上抄写了本教程( 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)

Edit: I have the main server file in /nodeexample and the client script in: /home/mysite/public_html/nodetest 编辑:我在/ nodeexample中有主服务器文件,在/ home / mysite / public_html / nodetest中有客户端脚本。

I did notice the app.get does not seem to be getting called. 我确实注意到app.get似乎没有被调用。 the final alert comes back as Also, you joined undefined 最后的警报返回为还,您加入了undefined

The error does not appear until the second refresh, and it seems as if it does work originally. 该错误将在第二次刷新后才会出现,并且看起来好像确实可以正常工作。

The Code I have is: 我拥有的代码是:

Server: 服务器:

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)

Client: 客户:

   <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>

I am not sure if I am missing a dependency or not? 我不确定我是否缺少依赖项? I loaded it with npm install express.io 我用npm install express.io加载了它

Any help is greatly appreciated! 任何帮助是极大的赞赏!

OK, so I figured it out finally, Ben led me on the right direction and it was due to the app.get function. 好的,我终于弄清楚了,Ben带领我朝着正确的方向前进,这归功于app.get函数。 It was a miss understanding of where the client file was suppose to be. 对于客户端文件应该位于的位置,这是一个错过的了解。

The req.sendfile will actually send the html to the port when it is opened. req.sendfile实际上会在打开时将html发送到端口。 I had the client file in public_html of my website where instead it should have been in the node server folder. 我的客户端文件位于网站的public_html中,而不是应该位于节点服务器文件夹中。

This way when you go to www.whateveryoursiteis.com:7076 it would load it load the html client for you. 这样,当您访问www.whateveryoursiteis.com:7076时,它将为您加载HTML客户端。

Files:
/node/server.js
/node/client.html

Run: node /node/server.js

Load: www.yoursite.com:7076

I figured this was a pretty simple mistake, this answer might help another newbie on his way 我认为这是一个非常简单的错误,此答案可能会对其他新手有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM