NodeJS | http | Doesn’t work on LAN

node.js

Code:

var express = require('express');
var connect = require('connect');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var path = require('path');

server.listen(3000, "0.0.0.0");

app.all('*', function(req, res) {
    if(req.originalUrl.slice(-3) == "css" || req.originalUrl.slice(-4) == "html" || 
        req.originalUrl.slice(-2) == "js" || req.originalUrl.slice(-3) == "png" || 
        req.originalUrl.slice(-3) == "jpg" || req.originalUrl.slice(-4) == "jpeg" || req.originalUrl.slice(-3) == "gif") {
        res.sendFile(__dirname + req.originalUrl);
    } else {
        res.sendFile(__dirname + req.originalUrl + "/index.html");
    }
});

io.sockets.on('connection', function(socket) {
    socket.on('sendMsg', function(data) {
        io.sockets.emit('newMsg', data);
    });

    socket.on('login', function(data) {
        if(data == "root") {
            socket.emit('authStatus', "Good nick");
        } else {
            socket.emit('authStatus', "Wrong nick");
        }
    });
});

Works 100% on localhost. But doesn't work on any other device on LAN. Can you help me, please?
EDIT: It don't even load site on another device on LAN.

Best Answer

It's hard to say what's wrong for sure but here are the things to check:

  • do you use the correct IP address?
  • can you ping yourself using that address from your own computer?
  • can you ping yourself using that address from other computers on the LAN?
  • do you have any firewall that blocks network requests? you need to have the port 3000 open
  • do you get the same error when you try to access http://YOUR-IP:3000/ from your own computer and from other computers on the LAN?
  • do you get the same error when you try to access http://YOUR-IP:3000/ (that should be open) and e.g. http://YOUR-IP:4000/ (that should be closed)?

You have to go through the list of possible issues one by one to narrow down the problem.