简体   繁体   中英

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.

I have literally carbon copied the tutorial ( https://github.com/techpines/express.io/tree/master/examples#sessions ) but I only receive this error:

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

I did notice the app.get does not seem to be getting called. the final alert comes back as Also, you joined 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

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. 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. I had the client file in public_html of my website where instead it should have been in the node server folder.

This way when you go to www.whateveryoursiteis.com:7076 it would load it load the html client for you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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