Confused with the given node.js code

nodejs

#1

Hello! When creating a new node.js project, in the index.html file it references socket.io.js like so

script src="/socket.io/socket.io.js"

However in the file directory, I don’t see any path that leads to that folder? How does the index.html get the socket.io.js file?

The reason I’m asking is because I’m trying to use socket.io in my own app and it keeps giving me on my browser console

GET https://projectname.c9users.io/socket.io/?EIO=3&transport=polling&t=LhCq26R 404 (Not Found)

on the server side, I have this setup

var express     = require('express');
var app         = express();
var server      = require('http').createServer(app);
var port        = process.env.PORT || 3000;
var hostname    = process.env.IP || "0.0.0.0";
var io          = require('socket.io').listen(server);
var path        = require('path');

app.get('/', function(req, res) {
   res.sendFile(path.join(__dirname + '/client/index.html'));
});

app.use('/client', express.static(__dirname + '/client'));
app.use(express.static(path.resolve(__dirname, 'client')));

app.listen(port || hostname, function () {
    console.log('Running the server on port ' + port);
});

var needSupport = [];
var finishedTasks = [];

io.on('connection', function (socket) {
  console.log("socket connected");
}); 

and on the index.html file i have this.

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script>
var socket = io.connect());

socket.on('connect', function () {
    console.log("connected!");
});
</script>

Can you help me out on why it’s not working? Thank you! I’m guessing it has to do something with me not having the http server listen to a port. I want to use express to make my web app though


#2

Okay found the problem. For one I had to use server.listen. Another thing is that I had to update the server socket.io to match the client socket. At least that’s what I think is the problem. It works now :slight_smile: