DEV Community

context menu
context menu

Posted on • Updated on

Hosting a Node.js HTTP server on your local internet

Intro

I was trying for a long time to figure out how to host a simple Node.js HTTP server on my internet, for testing purposes. However, I just couldn't find anything on how to do it, but then one day I discovered a simple way to do it.

This way does involve your private IP address, so if you're not comfortable with using it this method will not work for you. However, only people on the same internet as you will be able to access this server.

Requirements

  • Node.js installed
  • npm installed
  • A text editor (I used jEdit, but you can use whatever satisfies your needs)

Setup

In a folder of your choice, create a server.js file. We'll come back to this.

Now we need to make sure we have the http Node package installed.

You can always install it globally, this way you won't have to install it again. (I could be wrong on this, but I think when you install Node.js, it automatically comes with the HTTP package)

npm i -g http
Enter fullscreen mode Exit fullscreen mode

Programming!

Open your server.js file. In this file, you will want to simply create an HTTP server. If you don't know how to do this, the code is at the end.

Now on to finding your private IP address. To do this, you can simply run the command below (in command prompt):

ipconfig
Enter fullscreen mode Exit fullscreen mode

The output of this command:

Windows IP Configuration

Ethernet adapter Ethernet:

  Media...

... (some other things you don't need to pay attention to)

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : lan
   Link-local IPv6 Address . . . . . : xx00::0xx:x0x0:00x0:x00x%00
   IPv4 Address. . . . . . . . . . . : <YOUR_IP> **(this is the important one)**
   Subnet Mask . . . . . . . . . . . : 000.000.000.0
   Default Gateway . . . . . . . . . : 000.000.00.0
...
Enter fullscreen mode Exit fullscreen mode

Now that you've found that address, just replace the hostname variable's value with this address.

Example:

const hostname = '<YOUR_IP>';
Enter fullscreen mode Exit fullscreen mode

That's it! You can now run the command (in the folder that your server.js file is):

node server.js
Enter fullscreen mode Exit fullscreen mode

It should (if you included this part) say something like this:

Server running at http://<YOUR_IP>:3000
Enter fullscreen mode Exit fullscreen mode

Now whatever you chose to run on your server is on your local internet!

Final result

const http = require('http');

const hostname = '<YOUR_IP>';
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}`)
});
Enter fullscreen mode Exit fullscreen mode

Reference

These are pages or other articles that either directly contributed or lead me to my solution.

https://stackoverflow.com/questions/14293370/publish-node-js-server-on-the-internet

Top comments (0)