I am working on a simple socket.io project, and have hit a wall. When I added the script <script src="/socket.io/socket.io.js"></script><script type="text/javascript">var socket = io(); </script>
into the head, I was expecting the application to recognize this, but instead I got the error message GET http://localhost:5678/socket.io/socket.io.js
and Uncaught ReferenceError: io is not defined
in my console. I am using the express framework.
var express = require('express');
var router = express.Router();
var http = require('http').Server(express);
var io = require('socket.io')(http);
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'NodeIM' });
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server = http.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
module.exports = router;
HTML:
<html>
<head>
<title>NodeIM</title>
<link rel="stylesheet" href="/stylesheets/style.css">
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">var socket = io(); </script>
<style type="text/css"></style>
</head>
<body>
<h1>NodeIM</h1>
<p>Welcome to NodeIM</p>
<hr>
<ul id="messages"></ul>
<form id="im_form" action=""><input id="m" autocomplete="off"><button>Send</button></form>
</body>
</html>
NOTE: I've also tried to follow socket.io - ReferenceError: io is not defined and changed my src to src="http://localhost:5678/socket.io/socket.io.js"
, but it did not fix the issue. I've also tried to use Node.js socket.io.js not found or io not defined, which just showed me another way of requiring socket.io, but doesn't fix the ReferenceError
problem I am having