Search code examples
node.jsexpressserverroutesip

Express.JS server to connect to host on remote network


So I got this small express server running. I can connect it to other devices on my local network e.g. mobile and other PC. However when connecting over my 4g it does not work. Is there any reason for this? I am sure when I ping other private addresses on remote networks it has worked before, why not now?

Code:

const express = require("express");
const server = express();
const PORT = 3000

server.use(express.static("static"));

server.get("/", (req,res)=>{
    res.sendFile(__dirname +  "/pages/index.html")
});

server.listen(PORT, "0.0.0.0", (req,res) => {
    console.log("Listening on port ", PORT)
});

Any information would be apricated I have some networking experience (still a noob just studying) and this really does interest me.


Solution

  • I assume you are trying to reach the server via your local IP. But you are doing it with 4G (in your phone maybe), which means your request is going over the internet while your local IP is only valid in your network.

    Even if you are using your public IP, you would probably have to configure port forwarding on your router for it to know how to handle the incoming traffic for this port.