Question

After completing Code School's node.js course I followed this guide on how to set up a socket.io server with express 3, but when I try connecting to localhost:8080, it gives me "Cannot GET /".
My firewall is set to allow incoming and outgoing requests on port 8080, and I have the latest versions of express and socket.io installed. My code is as follows:

app.js

var express = require("express");
var socket = require("socket.io");

var app = express();
app.use(express.static(__dirname + "/public"));
var server = app.listen(8080);
var io = socket.listen(server);

index.html

<!doctype html>
<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            var server = io.connect("http://localhost:8080");
        </script>
    </head>
    <body>
        <p>Test</p>
    </body>
</html>

Directory structure

C:\Users\Joseph\Desktop\test\node_modules
                            \public\index.html
                            \app.js

EDIT:

This also does not work in app.js:

var http = require("http");
var express = require("express");
var socket = require("socket.io");

var app = express();
app.use(express.static(__dirname + "/public"));
var server = http.createServer(app);
server.listen(8080);
var io = socket.listen(server);
Was it helpful?

Solution

I discovered that I was getting the error because node is not smart enough to target index.htm by default from localhost:8080, and as such I needed to type localhost:8080/index.htm to access the page.

OTHER TIPS

Well If this is not working on windows then try doing this in your app.js:

var express = require("express");
var socket = require("socket.io");
var path = require('path');
 // path is a built-in modules

var app = express();
app.use(express.static(path.join(__dirname, 'public')));
var server = app.listen(8080);
var io = socket.listen(server);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top