Local Web Server Tips & Tricks
When developing web applications, static websites, or testing HTML, CSS, and JavaScript files locally, it's often useful to run a lightweight web server instead of opening files directly from the filesystem (file://).
Using a local web server helps avoid common issues such as:
- CORS restrictions
- JavaScript module loading errors
- AJAX/Fetch API failures
- Relative path issues
- Routing problems in modern web frameworks
This guide covers several easy ways to start a local web server.
Node.js HTTP Server
Install http-server
http-server is a simple, zero-configuration static web server for Node.js.
Install Globally
npm install -g http-server
Start Server Using Default Settings
http-server
By default, this serves the current directory and typically listens on:
http://localhost:8080
Specify Host and Port
http-server -a localhost -p 80
Options:
-a= Address to bind-p= Port number
Example:
http-server -a localhost -p 3000
Automatically Open Browser
http-server -o
Disable Caching During Development
http-server -c-1
This ensures browsers don't cache your files while testing.
Enable HTTPS
http-server -S -C cert.pem -K key.pem
Useful when testing features that require secure origins.
PHP Built-In Web Server
PHP includes a lightweight development web server.
Start Server
php -S localhost:8080
Browse to:
http://localhost:8080
Serve a Specific Directory
php -S localhost:8080 -t public
Example directory structure:
project/
├── public/
│ ├── index.php
│ └── css/
Router Script
Useful for Single Page Applications (SPA):
php -S localhost:8080 router.php
Example router:
<?php
if (file_exists(__DIR__ . $_SERVER['REQUEST_URI'])) {
return false;
}
include 'index.html';
Python HTTP Server
Python includes a built-in web server module.
Python 3 (Recommended)
Start Python 3 Server
python -m http.server
Default URL:
http://localhost:8000
Use a Custom Port
python -m http.server 8080
Bind to Localhost Only
python -m http.server 8080 --bind localhost
Serve a Specific Directory in Python Web Server
python -m http.server 8080 --directory ./dist
Python 2 (Legacy)
Older systems may still support:
python -m SimpleHTTPServer
Default URL:
http://localhost:8000
Note: Python 2 reached end-of-life in 2020 and should not be used for new development.
Visual Studio Code Live Server
For front-end development, VS Code's Live Server extension is extremely convenient.
Install Extension
Search for:
Live Server
in the Visual Studio Code Extensions marketplace.
Start VS Code Server
Right-click an HTML file and select:
Open with Live Server
Benefits:
- Automatic browser refresh
- Simple setup
- Supports static websites
- Great for HTML/CSS/JavaScript development
npx http-server
If you don't want to install globally:
npx http-server
Advantages:
- No global installation
- Uses project-local version if available
- Ideal for temporary testing
Ruby Simple HTTP Server
If Ruby is installed:
ruby -run -e httpd . -p 8080
Browse to:
http://localhost:8080
BusyBox HTTP Server (Linux)
Available on many Linux distributions and containers.
busybox httpd -f -p 8080
Useful for:
- Embedded systems
- Docker containers
- Lightweight environments
Docker-Based Static Web Server
Serve files using NGINX without installing additional tools.
Run NGINX Container
docker run --rm -it \
-p 8080:80 \
-v $(pwd):/usr/share/nginx/html:ro \
nginx
Browse to:
http://localhost:8080
Benefits:
- Consistent environment
- Mirrors production NGINX behavior
- No host dependencies
Common Development Ports
| Port | Common Use |
|---|---|
| 80 | Standard HTTP |
| 443 | Standard HTTPS |
| 3000 | React / Node.js |
| 4200 | Angular |
| 5173 | Vite |
| 5500 | VS Code Live Server |
| 8000 | Python Default |
| 8080 | General Development |
Troubleshooting
Port Already in Use
Error:
Address already in use
Use another port:
http-server -p 8081
Or find the process using the port.
Windows
netstat -ano | findstr :8080
Linux / macOS
lsof -i :8080
Browser Caching Issues
Force refresh:
Windows/Linux
Ctrl + F5
macOS
Cmd + Shift + R
Or disable caching in browser developer tools.
JavaScript Modules Not Working
Error:
Failed to load module script
Cause:
Opening files directly instead of using a web server.
Solution:
Use any local server described above.
Quick Reference
# Node.js
npm install -g http-server
http-server
# Node.js (temporary)
npx http-server
# PHP
php -S localhost:8080
# Python 3
python -m http.server
# Python 2 (legacy)
python -m SimpleHTTPServer
# Ruby
ruby -run -e httpd . -p 8080
# Docker + NGINX
docker run --rm -it -p 8080:80 -v $(pwd):/usr/share/nginx/html:ro nginx
Recommendation
For most developers:
- Front-end testing: VS Code Live Server
- Quick static hosting: Python
http.server - Node.js projects:
http-server - Containerized testing: NGINX with Docker
- PHP applications: PHP Built-In Server