Need to quickly serve static files locally or share them across your network? http-server is a simple, zero-configuration command-line HTTP server that makes this incredibly easy!
Whether you're testing a static website, sharing files with colleagues, or serving assets during development, http-server is the perfect lightweight solution.
What is http-server?
http-server is a simple, zero-configuration command-line static HTTP server built on Node.js. It's perfect for:
- Serving static websites locally
- Testing HTML, CSS, and JavaScript files
- Sharing files across your local network
- Quick prototyping and development
The best part? No configuration files, no complex setup - just install and run!
Installation
You can install http-server globally using npm:
npm install -g http-server
Or use it directly with npx (without global installation):
npx http-server
Basic Usage
Serve Current Directory
Navigate to your project folder and run:
http-server
This will:
- Serve files from the current directory
- Start the server on port 8080 by default
- Make it accessible at
http://localhost:8080
Serve Specific Directory
To serve files from a specific directory:
http-server ./my-website
Custom Port
Want to use a different port? Use the -p
flag:
http-server -p 3000
Now your server runs on http://localhost:3000
Network Access
Here's where http-server really shines - sharing files across your network!
Enable Network Access
Use the -a
flag to specify the host address:
http-server -a 0.0.0.0
This makes your server accessible to other devices on your network using your computer's IP address.
Find Your IP Address
# Find your IP address
ip addr show | grep inet
# or
ifconfig | grep inet
Once you know your IP (e.g., 192.168.1.100
), others can access your files at:
http://192.168.1.100:8080
Useful Options
Disable Caching
Perfect for development when you want to see changes immediately:
http-server -c-1
Enable CORS
Allow cross-origin requests:
http-server --cors
Custom Index File
Specify a different default file:
http-server -i custom-index.html
Silent Mode
Run without logging requests:
http-server -s
Practical Examples
Complete Development Setup
# Navigate to your project
cd my-project
# Start server with custom port, network access, no cache, and CORS
http-server -p 3000 -a 0.0.0.0 -c-1 --cors
Quick File Sharing
# Share files in current directory across network
http-server -a 0.0.0.0 -p 8080 -o
The -o
flag automatically opens the server in your default browser.
Production-like Testing
# Serve with gzip compression enabled
http-server -g
Pro Tips
1. Use Package.json Script
Add to your package.json
:
{
"scripts": {
"serve": "http-server -p 3000 -a 0.0.0.0 -c-1 --cors",
"dev": "http-server -o -c-1"
}
}
Then run with:
npm run serve
2. Quick Testing Script
Create a serve.sh
script:
#!/bin/bash
echo "Starting http-server..."
echo "Local: http://localhost:3000"
echo "Network: http://$(hostname -I | cut -d' ' -f1):3000"
http-server -p 3000 -a 0.0.0.0 -c-1 --cors
3. Combine with Live Reload
For a more complete development experience, consider combining with tools like live-server
or browser-sync
, but http-server remains the simplest option for basic static file serving.
Security Considerations
⚠️ Important: When using -a 0.0.0.0
, your files become accessible to anyone on your network. Only use this in trusted environments and never expose sensitive files.
For production use, consider proper web servers like Nginx or Apache with proper security configurations.
Conclusion
http-server is an incredibly useful tool for any web developer's toolkit. Its simplicity and zero-configuration approach make it perfect for:
- Quick local development
- Testing static sites
- Sharing files across your network
- Prototyping and demos
With just a single command, you can have a fully functional HTTP server running and accessible across your network. No complex setup, no configuration files - just simple, effective file serving.
Give it a try on your next project - you'll wonder how you lived without it!
Happy coding! 🚀