Jack TC
Jack TC

Reputation: 344

Node.js socket.io.js not found or io not defined

I'm trying to run a node.js application on my freebsd server, but I can't get the socket.io library to work with it. I've tried including:

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

Which gives a 404 error, and if i link directly to the file (i.e. where it is in my public_html folder) I get the io not defined error.

Thanks in advance

Upvotes: 4

Views: 13905

Answers (4)

SridharKritha
SridharKritha

Reputation: 9611

For those who got the same kind of issue if they run (open) your html file directly from your local file directory(ex: file:///C:/Users/index.html).

Solution: You have to run(open) the file through localhost (ex: http://localhost:3000/index.html) where the server is listening to.

Below code snippet shows how to create a server and how to wire together with the express and socket.io

const express = require("express");
const app = express();
const httpServer = require("http").createServer(app); 
const io = require("socket.io")(httpServer);

///////////////////////////////////////////////////////////////
// Any other server-side code goes here                     //
//////////////////////////////////////////////////////////////

httpServer.listen(3000, () => {
    console.log(`Server listening to port 3000`);
});

Upvotes: 0

sonikarc
sonikarc

Reputation: 137

Try creating another node.js application that has this single line in it and then run it with node.js

var io = require('socket.io').listen(8000);

Then in your browser visit http://127.0.0.1:8000 and you should get the friendly "Welcome to socket.io." greeting. If you are getting this then socket.io is running and will serve the socket.io.js file.

The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file. Unless you're running the socket.io server on express which is running on port 80. For now create a client file that has the script source for socket.io set to

<script src="http://127.0.0.1:8000/socket.io/socket.io.js"> </script>

This should connect to the socket.io server running on port 8000 and get the socket.io.js file.

Upvotes: 12

vimdude
vimdude

Reputation: 4585

Add the following after body parser:

, express.static(__dirname + "/public")

So something like:

var app = module.exports = express.createServer(
  express.bodyParser()
  , express.static(__dirname + "/public")
);

Upvotes: 0

Amadan
Amadan

Reputation: 198304

Your node.js application still has to serve it - it does not get served automagically. What do you have in your server? It should be something like

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

or similar (the listen is important). The location is not a real location on the disk - socket.io library should intercept the URL and serve its clientside library, as far as I understand.

Upvotes: 3

Related Questions