Making your Node.js application accessible on a local network can be very beneficial for testing, debugging, and evaluating the overall performance of your application.

Here’s how you can make it work:

1. Check Network Configuration

To get started, you need to ensure that your computer or server is connected to the local network where you want the Node.js app to be accessible.

2. Determine local IP address

To find the IP address of your computer, go to command prompt (for windows) and enter the following command:

> ipconfig

When you enter this command, you’re going to have some messages as response. The only thing you need to look for, is the line that says “IPv4 Address”.

Copy the value of the IPv4 Address. You’re going to need it in your code.

3. Update app configuration to listen on the local IP address

Supposing the following “Hello World” Node.js application is what you want to make accessible on a local network:

const http = require("http");

const hostname = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World");
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

You have to update the hostname in the code to the IP address of your computer (the IP address obtained from the previous step).

const hostname = "YOUR_IPV4_ADDRESS";

Where:

  • YOUR_IPV4_ADDRESS is the value of the IPv4 address you copied from your command prompt. Example: 192.168.0.3

4. Test Access

You can now go on one of the devices on your local network and enter the following address in the web browser:

http://your.network.ip.address:port/

Where “your.network.ip.address” is the value of the IPv4 address you copied from your command prompt.

Note: If you’re having any trouble, check to ensure that your computer’s firewall allows incoming traffic on the specific port that your app is listening on.