1

I'm making a web-app that makes use of nodejs, mongodb, socket.io, express and mongoose.
I can start my server and correctly get the wanted html file in my browser when browsing to my localhost.
The problem I have is getting my socket.io to work. On my server side everything works fine : I get " info - socket.io started " in my terminal.

But when surfing to my browser I get this in my browser console

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

This is how i connect to socket.io.js

    <script src="/socket.io/socket.io.js"></script>

and my map structure looks like this:

 map
  -app.js
  -public
  --index.html

  -node_modules
  --socket.io
  --mongodb
  --express
  --jade
  --mongoose

Does anyone knows what the mistake is I've made?

(it's actually the same problem as here: Node.js socket.io.js not found or io not defined )

Thanks in advance!

EDIT: My code on the server side is this:

    var app= require('express').createServer();
    var io = require('socket.io').listen(app);
    var mongoose = require('mongoose');
    var db = mongoose.connect('mongodb://localhost/db');

    app.listen(3030);
    console.log("server started");

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


    io.sockets.on('connection',function(socket){    
    console.log("connection made");
    });

The first log, gets logged in the terminal ("server started"), but the second one ("connection made") doesn't get logged. So the connection isn't made. I thought that was because of the wrong "set up" in my client side.

Community
  • 1
  • 1
Decor
  • 548
  • 1
  • 7
  • 20
  • If it's the same problem as [Node.js socket.io.js not found or io not defined](http://stackoverflow.com/questions/10563080/node-js-socket-io-js-not-found-or-io-not-defined), have you tried the answers to that question? – phihag Aug 13 '12 at 17:19
  • Can you post your code regarding Socket.IO setup on the server-side? The typical problem here is that Socket.IO isn't listening (aka set up is wrong) for requests for the file. Without seeing your code, we can't rule that out. – Toby Lawrence Aug 13 '12 at 17:21
  • For me, this is often caused by not running the application at the root of the domain. Meaning this works fine (http://domain.com) but this would cause 404 without some modifications to the client code (http://domain.com/path/to/app/. – Timothy Strimple Aug 13 '12 at 17:44
  • @phihag Yes, I did try them. But as you can conclude from the answers of the author, they didn't worked for him either. – Decor Aug 13 '12 at 17:52
  • Which version of express are you using? – Chris Aug 13 '12 at 17:58

2 Answers2

2

Check out the express migration guide 2->3 https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x Something like this should work

 var express = require('express');
 var app = express();
 var http = require('http');
 var server = http.createServer(app);
 var io = require('socket.io').listen(server);
 var mongoose = require('mongoose');
 var db = mongoose.connect('mongodb://localhost/db');

 server.listen(3030);
 console.log("server started");

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


 io.sockets.on('connection',function(socket){    
    console.log("connection made");
 });
Chris
  • 4,204
  • 1
  • 25
  • 30
  • 1
    Wow, Chris, thanks! this worked. But I had to make a couple of changes: The first two lines should be `var express = require('express');` `var app = express;` and from there on it works! Thank you! – Decor Aug 13 '12 at 18:22
-1
var app = express();
app.set('port', process.env.PORT || 3000);

...

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

var io = socket.listen(server);
io.sockets.on('connection', function () {
  console.log('hello world im a hot socket');
});
besch
  • 45
  • 4