I am using the following example code from https://github.com/LearnBoost/socket.io to start a Socket.IO and Express example.
When I got to localhost, I get this error:
Cannot GET /
My app.js looks like this:
var express = require('express');
var io = require('socket.io');
var server = require('http');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
server.listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
My index.html looks like this:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Packages:
npm ls
├─┬ express@3.0.0rc2
│ ├── commander@0.6.1
│ ├─┬ connect@2.4.2
│ │ ├── bytes@0.1.0
│ │ ├── formidable@1.0.11
│ │ ├── pause@0.0.1
│ │ └── qs@0.4.2
│ ├── cookie@0.0.4
│ ├── crc@0.2.0
│ ├── debug@0.7.0
│ ├── fresh@0.1.0
│ ├── methods@0.0.1
│ ├── mkdirp@0.3.3
│ ├── range-parser@0.0.4
│ └─┬ send@0.0.3
│ └── mime@1.2.6
└─┬ socket.io@0.9.10
├── policyfile@0.0.4
├─┬ redis@0.7.2
│ └── hiredis@0.1.14
└─┬ socket.io-client@0.9.10
├─┬ active-x-obfuscator@0.0.1
│ └── zeparser@0.0.5
├── uglify-js@1.2.5
├─┬ ws@0.4.21
│ ├── commander@0.6.1
│ ├── options@0.0.3
│ └── tinycolor@0.0.1
└── xmlhttprequest@1.4.2
Thanks!!
You've not instructed Express to serve your index.html
file. Probably the easiest way to get it running is to make a directory called public
and put index.html
in it, and modify your Express server so that it serves static files from that directory:
var express = require('express');
var io = require('socket.io');
var server = require('http');
var path = require('path');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
app.use(express.static(path.join(__dirname, 'public')));
And then visit localhost/index.html
in the browser.