Spin Up a Local HTTP Server with Python or Node.js

In this guide, we'll show you how to create a local HTTP server using Python's SimpleHTTPServer or the npm package http-server
Published 2023-05-07 5 min read
Spin Up a Local HTTP Server with Python or Node.js

Need to quickly set up a local HTTP server for testing or sharing files? There are easy solutions to achieve just that. In this article, we’ll walk you through the steps to spin up a local HTTP server, expose it to your local network, and even share it on the internet. Let’s get started!

 

How to spin up a simple HTTP server?

There are two main routes. One is a build in Python module another one is a NPM package. Let’s start with Python.

SimpleHTTPServer

First, you’ll need Python installed on your system. To check if you already have it, open your terminal or command prompt and type:

python --version
# or
python3 –version

If you see a version number, you’re good to go. To be honest, Python should be installed in your system.  If not, head over to the official Python website to download and install Python.

Now that you have Python installed, starting a SimpleHTTPServer is super easy. Just open your terminal or command prompt and run the following command:

# For Python 2.x:
python -m SimpleHTTPServer

# For Python 3.x:
python -m http.server
# or
python3 -m http.server

This will start a local HTTP server on port 8000 by default, you can change it with the following:

python -m http.server 8080

 

Served files depend on where you spin up your server, this location will become the www root directory. If you wish to change a www root either navigate to the desired directory or use —directory option (works only for Python 3.7+):

cd /path/to/your/directory
python -m http.server
# or
python -m http.server –directory /path/to/your/directory

http-server

NPM alternative is a package http-server. It’s also zero-configuration, but has more options to play around with, as it supports compression, basic auth, or CORS, to read more on this visit the project page https://github.com/http-party/http-server.

Assuming you have node installed just install the package with this command:

npm install -g http-server

and then run it with:

http-server

Note that the default port is 8080 and the server binds to your local IP automatically, which is convenient, because the IP is displayed when the server starts.

Access the server locally and from the local network

To access your new local server, open your browser and type http://localhost:8000 or http://localhost:8080 in the address bar. You should see the contents of your current directory listed. That’s it – your local HTTP server is up and running!

Your server is also available in your local network. To find you what’s your local IP use the following:

On Windows, open the command prompt and type ipconfig. Your local IP address is listed as “IPv4 Address.”

On macOS or Linux, open the terminal and type ifconfig. Your local IP address is listed as ”inet addr” or ”inet” under the active network interface.

If there is index.html file in your directory it’ll be rendered as a webpage. Additionally, JS http-server will use 404.html for “not found” files. Otherwise, you’ll get a files list, like this:

This is how a list would look like

 

Exposing SimpleHTTPServer to the Internet

Preparations

To make your server accessible from the internet, you’ll need to set up port forwarding on your router. This process directs external traffic to your local server. Since each router’s settings are different, we recommend checking your router’s manual or doing a quick online search for instructions.

Once you’ve found your router’s port forwarding settings, forward the port you used for server (e.g., 8000) to your local IP address (e.g., 192.168.1.100).

Next, find your public IP address by visiting a website like WhatIsMyIP.com or simply searching “what is my IP” on Google.

Access the server from the internet

Now that you’ve set up port forwarding, your server should be accessible from the internet. Share your public IP address and the port number (e.g., http://203.0.113.42:8000) with others so they can access your server through their browser.

Security considerations

Remember that exposing your server to the internet comes with security risks. Ensure you only share your server with trusted individuals and don’t leave it running when it’s unnecessary. Both solutions should be used with caution in such a case. Don’t expose anything vulnerable.

 

Other tips

Running the server in the background

To run SimpleHTTPServer in the background, you can use the following commands:

 

On macOS or Linux:

python -m http.server &>/dev/null &

On Windows:

Start-Process -NoNewWindow "python -m http.server"

You can use the same approach to http-server package.

 

Keeping the server running after closing the terminal (Linux and macOS)

To keep the server running even after closing the terminal, you can use the nohup command on Linux and macOS:

nohup python -m http.server &>/dev/null &

 

There you have it! Now you know how to spin up a local HTTP server using Python’s SimpleHTTPServer or JavaScript http-server, expose it to your local network, and even share it online. Always consider security risks when exposing your server to the internet and only share it with trusted individuals.

#cli