Failed to load resource


I'm trying to getting started with and node.js.

Following the first example on the's site I'm getting the following error in the browser's console:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3001/
Uncaught ReferenceError: io is not defined 

This is my server.js

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


app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {

And this is my index.html

<!DOCTYPE html>
    <meta charset="UTF-8" />
    <script src="/"></script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    socket.emit('my other event', { my: 'data' });

I've already installed

7/25/2012 3:27:42 PM

The Issues

  • First of all you need to be looking at the server port that the server is bound on (app.listen(3001);) on the client side in order to reach the server at all.

  • As for, adding http://localhost:3001 before the rest of the source in the link tag solves this problem. This is apparently due to the way the network binds ports to localhost, however I will try to find some more information on the cause;

What to change:

The port binding for the server:

var socket = io.connect('http://localhost');

should be change to

var socket = io.connect('http://localhost:3001');

Making behave:

<script src="/"></script>

should be change to

<script src="http://localhost:3001/"></script>

7/26/2012 7:54:27 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow