someRandomSerbianGuy
someRandomSerbianGuy

Reputation: 491

Client-side cannot locate socket.io.js

I've read all other posts on this topic but I cannot find answer for my problem. I have Express server running on default port or 5000 (process.env.PORT || 5000). I then added socket.io but client-side cannot locate socket.io.js file. After some testings I assume that there is problem with some sort of port conflicts or something. Here is my server-side code:

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

    app.set('port', (process.env.PORT || 5000));

Edit: Client-side stuff:enter image description here

Edit 2: Also, I don't think that it's important but I'm testing this on Heroku local.

Edit 3: Directory structure:

--api
--images
--node_modules
--scripts
--pages
    main_page.ejs
--styles
composer
index.js 
npm-debug
package

Edit 4: Bump

Upvotes: 4

Views: 637

Answers (1)

Joshua Kleveter
Joshua Kleveter

Reputation: 1769

Problem

Socket.IO is unable to intercept the requests to /socket.io/ that load socket.io.js. This means that the virtual socket.io.js file is never loaded and you get the aforementioned 404 error.

Solution

Change this:

app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port', function() { ... });

To this:

app.set('port', (process.env.PORT || 5000));
server.listen(app.get('port'), function() { ... });

This prevents Express from immediately intercepting the requests to /socket.io/ and allows the file to load.

Upvotes: 2

Related Questions