Skip to content

YounessBrunno/vanilla-node.js-static-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vanilla Node.js Static File Server

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.


Features

  • Static File Serving – Serves HTML, CSS, JS, images, and other file types.
  • MIME Type Detection – Automatically sets correct Content-Type headers 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 uncaughtException and unhandledRejection globally.
  • Configurable via config.json – Easily modify ports, root directory, log files, fallback MIME type, and error page.

📂 Project Structure

  • logs/ – Stores access & error logs
    • access.log
    • error.log
  • public/ – Static files
    • 404.html
    • index.html
    • main.css
    • script.js
    • img.png
  • utilities/ – Helper utilities
    • writeLog.js
  • .env.local – Optional environment variables
  • .gitignore – Git ignore rules
  • config.json – Server configuration
  • mimeTypes.js – MIME type mappings
  • package.json – Project metadata & dependencies
  • package-lock.json
  • server.js – Main server implementation
  • README.md

⚙️ Configuration

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"
  }
}

🛠 Installation

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

▶️ Usage

# 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).

About

Custom static file server built from scratch in Node.js (no frameworks). Features MIME type detection, efficient file streaming, dynamic directory listings, custom 404 error pages. a lightweight Apache-style server implemented entirely in vanilla Node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors