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.

Thi…


This content originally appeared on DEV Community and was authored by context menu

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.

npm i -g http

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 FIX THIS

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

ipconfig

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
...

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

Example:

const hostname = '<YOUR_IP>';

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

node server.js

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

Server running at http://<YOUR_IP>:3000

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}`)
});


This content originally appeared on DEV Community and was authored by context menu


Print Share Comment Cite Upload Translate Updates
APA

context menu | Sciencx (2021-04-12T23:13:51+00:00) Hosting a Node.js HTTP server on your local internet. Retrieved from https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/

MLA
" » Hosting a Node.js HTTP server on your local internet." context menu | Sciencx - Monday April 12, 2021, https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/
HARVARD
context menu | Sciencx Monday April 12, 2021 » Hosting a Node.js HTTP server on your local internet., viewed ,<https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/>
VANCOUVER
context menu | Sciencx - » Hosting a Node.js HTTP server on your local internet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/
CHICAGO
" » Hosting a Node.js HTTP server on your local internet." context menu | Sciencx - Accessed . https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/
IEEE
" » Hosting a Node.js HTTP server on your local internet." context menu | Sciencx [Online]. Available: https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/. [Accessed: ]
rf:citation
» Hosting a Node.js HTTP server on your local internet | context menu | Sciencx | https://www.scien.cx/2021/04/12/hosting-a-node-js-http-server-on-your-local-internet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.