Question

i use code from examples of socket.io site and has some problem

My server code (on debian 192.168.5.200)

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

server.listen(1337);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

My client code (index.html)

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

i start node server open in browser http://{host ip}:1337 and... got 404 on socket.io connection

it try to get "/api/1/?t=..." url and got answer by express "Cannot get /api/1/?t=..." with 404 error

Please help me (

Was it helpful?

Solution

Try to change your client code to:

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

Also, in order to be able to go to /api/1... you need to register the corresponding app.get, e.g. as app.get("/api/*", ..., which would handle all the connections to /api/.... Otherwise it is expected that you will be getting the 404 error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top