3
votes

I got an issue with Express.io, I try to create a simple tchat. But I'm not be able to include the socket.io.js, I got an error...

I just installed Express.io on my new Express project.

My errors :

  1. GET http://***/socket.io/socket.io.js 404 (Not Found)
  2. localhost:1 Uncaught ReferenceError: io is not defined

Index.jade

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content
    script(src="http://localhost:3000/socket.io/socket.io.js")
    script(src="/javascripts/user.js")

app.js

/**
 * Module dependencies.
 */

var express = require('express.io')
    , index = require('./routes/index.js')
    , http = require('http')
    , path = require('path');

var app = express();

app.http().io();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', index.index);

app.io.route('ready', function(req) {
    req.io.emit('talk', {
        message: 'io event from an io route on the server'
    });
});

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

index.js (route)

exports.index = function(req, res){
    res.render('index', {
        title: 'TCHAT'
    });
};

user.js

io = io.connect();

// Emit ready event.
io.emit('ready');

// Listen for the talk event.
io.on('talk', function(data) {
    alert(data.message);
});

New error

Failed to load resource: the server responded with a status of 404 (Not Found) http://*:3000/socket.io.js Uncaught ReferenceError: io is not defined

1
What's your node version? While trying to replicate I got an npm install error during socket.io-client on node 0.6.18 which could explain things if you're on 0.6.xgeneralhenry
Try including socket.io.js from root, as you're doing with user.js: script(src="/socket.io/socket.io.js").Jonathan Lonowski
Nearly the same thing... See above. (new error)tonymx227
I don't understand what I'm supposed to do ?! Remove socket.io ?tonymx227

1 Answers

9
votes

Dude, you are listening on http server not express.io server. Change this

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

to this :

app.listen(app.get('port'), function(){
   console.log("Express server listening on port " + app.get('port'));
});

then you will be able to access socket.io.js