I am new to socket.io. It sounds great but nothing works for me.
I have two file listed below: app.js and index.html. I have them both in the same directory. I cd to that directory and run:
node app.js
and then I go to my browser and visit http://hostname:8888 and I get:
Cannot GET /
so I try http://hostname:8888/index.html and get:
Cannot GET /index.html
Why is it not working and why am I getting
npm ERR! invalid: socket.io-client@0.9.11
Please someone help. Thank you.
// app.js
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
server.listen(8888);
// index.html
<!DOCTYPE html>
<html lang=en-ca>
<head>
<meta charset=utf-8>
<title>To Do List</title>
</head>
<body>
<script src="http://localhost:8888/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8888');
</script>
</body>
</html>
npm ERR! invalid: socket.io-client@0.9.11 /home/username/node_modules/socket.io-client
npm ERR! not ok code 0
You write about your app.js and index.html:
I have them both in the same directory.
But your code says:
app.use(express.static(__dirname + '/public'));
Which basically means: "my index.html file should be found in a directory named public which can be found in the same directory as app.js". So it's expecting this directory structure:
directory/app.js
directory/public/index.html
So either create an public directory and move your index.html there, or tell express.static to find it in the same directory as app.js like so:
app.use(express.static(__dirname));
(Although, as @josh3736 points out in the comments, that introduces security issues so you don't want to use that solution in any production code! For testing purposes though, it's okay.)
As for your other problem, it's unclear what's causing that error. Please add a little more npm output.
However, you don't need to install the socket.io-client package if you're going to run the socket.io client from your browser, since it's already part of the socket.io package (the -client package is meant to be used to implement a client from Node itself).