Friday 17 March 2017

nodejs, socket.io and expressjs not connecting

I have a folder structure like:

socket
  - project
    - main.js

  -node_modules
    - node_modules_folder_here

  - text.html

My main.js file looks like:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    console.log("inside")
    res.send('<h1>Hello again</h1>');
});

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

http.listen(3000, function(){
  console.log('listening on *:3000');
}); 

My test.html looks like:

<body>
<h3>Hellooww world</h3>
<script src="localhost:3000"></script>
<script type="text/javascript">
  var socket = io();
  console.log("check io", socket)
</script>
</body>
</html>

I simply followed socket.io tutorial and got this error

GET file:///socket.io/socket.io.js net::ERR_FILE_NOT_FOUND
test.html:10 Uncaught ReferenceError: io is not defined

What is the issue here ? I am simply following the socket.io tutorial and what am I mising here ? What is wrong and why ??



via aryan

No comments:

Post a Comment