2013-05-26 85 views
4

我試圖從我的節點服務器解析'/socket.io/socket.io.js'時收到404。我無法確定爲什麼。Socket.io和Express3 - 404客戶端請求到/socket.io/socket.io.js

我的服務器配置是這樣的:

var express = require('express') 
    , engine = require('ejs-locals') 
    , app = express() 
    , http = require('http') 
    , server = http.createServer(app) 
    , io = require('socket.io').listen(server); 


// use ejs-locals for all ejs templates: 
app.engine('ejs', engine); 

app.set('views',__dirname + '/views'); 
app.set('view engine', 'ejs'); // so you can render('index') 
app.use(express.static(__dirname + '/public')); 

//define routes 
... 

app.listen(3000); 



//socket io 
io.sockets.on('connection', function (socket) { 
    socket.on('set nickname', function (name) { 
    socket.set('nickname', name, function() { 
     socket.emit('ready'); 
    }); 
    }); 

    socket.on('msg', function() { 
    socket.get('nickname', function (err, name) { 
     console.log('Chat message by ', name); 
    }); 
    }); 
}); 

在客戶端,我有這樣的:

<script src="/socket.io/socket.io.js"></script> 
<script> 
    var socket = io.connect('http://localhost:3000'); 
    socket.on('news', function (data) { 
    console.log(data); 
    socket.emit('my other event', { my: 'data' }); 
    }); 
</script> 

我在本地測試這個(端口:3000)。 Socket代碼基本上是從socket.io示例中剝離出來的,以獲得某些工作。我審查了其他職位,似乎無法找到我要去哪裏錯了。有人可以幫忙嗎?

謝謝

回答

7

知道了!

HTTP服務器需要在端口上列出(在這種情況下爲3000)。然後我刪除了app.listen(3000),因爲該地址已經被使用且不需要。

謝謝!

var express = require('express') 
    , engine = require('ejs-locals') 
    , app = express() 
    , http = require('http') 
    , server = http.createServer(app).listen(3000) 
    , io = require('socket.io').listen(server);