Error node js socket io socket io js not found

0 votes

Getting the error:

 /socket.io/socket.io.js 404 (Not Found) Uncaught ReferenceError: io is not defined

My code is:

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

server.listen(3000);

and

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

How to fix it?

Nov 27, 2020 in Node-js by kartik
• 37,520 points
14,774 views

1 answer to this question.

0 votes

Hello @kartik,

Copying socket.io.js to a public folder (something as resources/js/socket.io.js) is not the proper way to do it.

If Socket.io server listens properly to your HTTP server, it will automatically serve the client file to via http://localhost:<port>/socket.io/socket.io.js, you don't need to find it or copy in a publicly accessible folder as resources/js/socket.io.js & serve it manually.

Code sample
Express 3 requires that you instantiate a http.Server to attach socket.io to first

var express = require('express')
  , http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

//... 

server.listen(8000);

Hope it helps!!

Get your Node.js Certification today to become a certified expert.

Thank you!!

answered Nov 27, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
1 answer

Error:sudo: npm: command not found

Hello @kartik, I had the same problem; here ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
12,554 views
0 votes
1 answer
0 votes
1 answer

Error: listen EACCES 0.0.0.0:80 OSx Node.js

Hello @kartik, Give root access to node and ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
5,622 views
0 votes
1 answer

Error:node' is not recognized as an internal or external command

Hello @kartik, Node is missing from the SYSTEM ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
8,361 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
4,190 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,243 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,116 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,028 views
0 votes
1 answer

Error:npm-cli.js not found when running npm

Hello @kartik, You need to run this in ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
19,341 views
0 votes
2 answers

Error:'TypeError: is not a function' in Node.js

You are exporting module.exports.redir = redir; That means that ...READ MORE

answered Oct 21, 2020 in Node-js by anonymous
• 140 points
17,282 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP