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

I am trying to run the node.js application on my freebsd server, but I cannot get the socket.io library to work with it. I tried including:

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

Which gives error 404, and if I refer directly to the file (i.e. where it is in my public_html folder), I get an undefined error.

Thanks in advance

+4
source share
3 answers

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

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

Then in your browser go to http://127.0.0.1:8000 and you should get the friendly "Welcome to socket.io". greeting. If you get this, socket.io starts up and will serve the socket.io.js file.

The only thing I can think of is that you cannot reference the alternate port in your client file. If you are not using socket.io server on express, which runs on port 80. For now, create a client file with the script source for socket.io set to

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

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

+10
source

The node.js application should still serve it - it does not automatically receive it. What is on your server? It should be something like

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

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

+3
source

Add the following after the parser:

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

So something like:

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

Source: https://habr.com/ru/post/1412134/


All Articles