How to Run a Local Web Server in One Command
Serve the files in any folder over HTTP with Python's built-in http.server or npx serve, open the site at localhost, and stop the server when done.
You have an HTML file or a folder of static files and want to view them the way a browser would load them from a real site. Opening the file directly often breaks relative paths and fetch requests. A local server fixes that, and both Python and Node include one.
Open a terminal in the folder
cd path/to/your/site
The server serves whatever directory it is started in, with index.html as the default page.
Start the server
With Python, which is preinstalled on macOS and most Linux distributions:
python3 -m http.server 8000
On Windows, use python -m http.server 8000. The last argument is the port; pick another number if 8000 is already in use.
With Node.js instead, npx runs the serve package without a permanent install:
npx serve
It prints the local address, port 3000 by default.
Open it in the browser
Go to:
http://localhost:8000
The folder's index.html loads. If there is no index file, the server shows a directory listing instead. Every request is logged in the terminal, which helps when a file returns 404.
Stop the server
Press Ctrl + C in the terminal window. The port is released immediately and the terminal returns to a normal prompt. Leave the server running while you edit; refresh the browser to see changes.
This serves static files only. http.server and serve hand back HTML, CSS, JavaScript, and images exactly as they are on disk. PHP, Python, or Node backend code will not execute. Use the framework's own dev server for that.
To open the site from a phone. localhost only works on the machine running the server. On another device on the same Wi-Fi network, use the computer's local IP address instead, for example http://192.168.1.20:8000. Find the address in your network settings, or with ipconfig on Windows.
More Coding how-tos
- How to Add a .gitignore File
- How to Check Which Node.js Version Is Installed
- How to Check Which Python Version Is Installed
- How to Clone a Git Repository
- How to Create a New Git Branch
- How to Create a Python Virtual Environment
- How to Install Node.js with nvm
- How to Make a Script Executable in the Terminal
- How to Print Hello World in JavaScript
- How to Print Hello World in Python
- How to Undo the Last Git Commit