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示例中剝離出來的,以獲得某些工作。我審查了其他職位,似乎無法找到我要去哪裏錯了。有人可以幫忙嗎?
謝謝