A custom static file server built entirely in vanilla Node.js (no frameworks). This project implements core server functionalities similar to Apache or Nginx, demonstrating low-level Node.js expertise.
- ✅ Static File Serving – Serves HTML, CSS, JS, images, and other file types.
- ✅ MIME Type Detection – Automatically sets correct
Content-Typeheaders using the mime package. - ✅ Directory Listing – Dynamic index pages with file/folder icons (📄 / 📁).
- ✅ Custom 404 Page – Handles missing files with a user-defined error page.
- ✅ Error Logging – Logs errors and access requests separately in
/logs. - ✅ Graceful Error Handling – Handles
uncaughtExceptionandunhandledRejectionglobally. - ✅ Configurable via
config.json– Easily modify ports, root directory, log files, fallback MIME type, and error page.
logs/– Stores access & error logsaccess.logerror.log
public/– Static files404.htmlindex.htmlmain.cssscript.jsimg.png
utilities/– Helper utilitieswriteLog.js
.env.local– Optional environment variables.gitignore– Git ignore rulesconfig.json– Server configurationmimeTypes.js– MIME type mappingspackage.json– Project metadata & dependenciespackage-lock.jsonserver.js– Main server implementationREADME.md
Modify config.json to change server settings:
{
"port": 3000,
"root": "./public",
"errorPage": "404.html",
"fallbackMime": "application/octet-stream",
"enableGzip": true,
"logs": {
"access": "./logs/access.log",
"error": "./logs/error.log"
}
}Follow these steps to set up and run the server locally:
# 1. Clone the repository
git clone https://github.com/yourusername/node-static-server.git
cd node-static-server
# 2. Install dependencies
npm install mime dotenv
# 3. (Optional) Create .env.local file for custom environment variables
# Example:
# PORT=3000
# ROOT_DIR=public
# 4. (Optional) Edit config.json to change:
# - Server port
# - Static files directory (rootDir)
# - Logs directory (logsDir)
# - Custom 404 page path
# - Fallback MIME type
# 5. Run the server
node server.js
# 6. Access in your browser
# http://localhost:3000
# Start the server
node server.js
# The server will:
# - Read the port from .env.local or config.json
# - Serve files from the /public directory by default
# Open in your browser:
http://localhost:3000
# Logs:
# - Access logs: logs/access.log
# - Error logs: logs/error.log
# To customize:
# - Edit config.json for port, rootDir, logsDir, custom 404 page, and fallback MIME type.
# - Use .env.local to override defaults quickly (example: PORT=4000).