1

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が呼び出されていないようです。最終アラートはまた、未定義に参加しました

エラーは 2 回目の更新まで表示されず、本来は機能しているように見えます。

私が持っているコードは次のとおりです。

サーバ:

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でロードしました

どんな助けでも大歓迎です!

4

1 に答える 1

1

OK、最終的にはわかりました。Ben は私を正しい方向に導きました。それはapp.get関数によるものでした。クライアントファイルがどこにあると想定されているかを理解していませんでした。

req.sendfileは、開いたときに実際にポートに html を送信します。私のウェブサイトのpublic_htmlにクライアントファイルがありましたが、代わりにノードサーバーフォルダーにあるはずでした。

この方法で www.whateveryoursiteis.com:7076 にアクセスすると、html クライアントが読み込まれます。

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

Run: node /node/server.js

Load: www.yoursite.com:7076

これはかなり単純な間違いだと思いました。この答えは、途中の別の初心者に役立つかもしれません

于 2013-10-20T19:33:16.050 に答える