Reputation: 1095
I'm trying to use express to serve static pages with express that communicate with socket.io. I keep running into problem after problem. I have done so many things, and googled so many times, that I can't believe I haven't found an answer to my specific problem. I keep running into places that show you the server, or show you the client, but not the other that communicates with it. Or both, but not with express. When I throw express into the mix, I start getting 404s and I them move my socket.io client scripts around and still can't seem to get it to work. This is where I'm at right now, though this is about my 20th configuration.
Server:
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function () {
app.use(express.static('html'));
});
app.listen(8080);
io.sockets.on('connection', function (socket) {
console.log('A socket connected!');
});
Client:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.send('testing');
</script>
I have tried changing the src to /html/socket.io.js (and copied all necessary files from the dist directory to /html), tried creating a scripts directory in the html directory, tried linking to socket.io's cdn, and I keep getting 404s. I know it's something stupid and simple that I'm messing up, I just can't figure it out for the life of me. Thanks!
edit: all I want to do right now is serve the index.html page and print to the server console when it sees the connection.
Upvotes: 2
Views: 2119
Reputation: 64312
Just have a look at the examples from here. I got it to work with express 3 by changing app.listen
to server.listen
. I think the routing that socket.io sets up is specific to the http server and does not use the express routing mechanism.
Upvotes: 8