Skip to content

supritR21/WebServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

WebServer

A lightweight Node.js HTTP server that serves static web pages with built-in event logging and error tracking capabilities.

πŸš€ Features

  • HTTP Server: Built with Node.js native http module
  • Static File Serving: Serves HTML, CSS, images, and JSON files
  • Event Logging: Advanced logging system with timestamps and unique IDs
  • Error Tracking: Automatic error logging to separate log files
  • Multi-route Support: Handle different URL routes and serve appropriate content
  • Content Type Detection: Automatic content-type handling for different file types
  • Development Mode: Hot reload support with Nodemon

πŸ“ Project Structure

WebServer/
β”œβ”€β”€ server.js           # Main server file
β”œβ”€β”€ logEvents.js        # Event logging utility
β”œβ”€β”€ package.json        # Project dependencies
β”œβ”€β”€ package-lock.json   # Dependency lock file
β”œβ”€β”€ README.md          # Project documentation
β”œβ”€β”€ views/             # HTML templates
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ greeting.html
β”‚   β”œβ”€β”€ 404.html
β”‚   β”œβ”€β”€ new-page.html
β”‚   └── subdir/
β”œβ”€β”€ css/               # Stylesheets
β”œβ”€β”€ img/               # Image assets
β”œβ”€β”€ data/              # Data files
└── logs/              # Log files (generated at runtime)

πŸ› οΈ Technologies Used

  • Node.js: JavaScript runtime environment
  • Built-in Modules: http, fs, path, events
  • date-fns (^4.1.0): Date formatting and manipulation
  • uuid (^11.0.4): Unique identifier generation
  • Nodemon (^3.1.9): Development server with auto-reload

πŸ“‹ Prerequisites

  • Node.js (v14 or higher)
  • npm (comes with Node.js)

βš™οΈ Installation

  1. Clone the Repository

    git clone https://github.com/supritR21/WebServer.git
    cd WebServer
  2. Install Dependencies

    npm install

πŸƒ Running the Server

Development Mode (with auto-reload)

npm run dev

Uses Nodemon to automatically restart the server on file changes.

Production Mode

npm start

Runs the server directly with Node.js.

Custom Port

PORT=5000 npm start

Default port is 3500.

πŸ“– Usage

Once the server is running, access it in your browser:

  • Home Page: http://localhost:3500/
  • Greeting Page: http://localhost:3500/greeting.html
  • Another Page: http://localhost:3500/new-page.html
  • API Endpoint: http://localhost:3500/api/data (if configured)

πŸ“ How It Works

Server Routing

The server uses URL routing to serve different files:

  • Maps request URLs to corresponding file paths in the views/ directory
  • Returns 200 status for found files, 400 for 404.html
  • Serves images and binary files without UTF-8 encoding

Event Logging System

The logging system (logEvents.js) provides:

  • Timestamps: Formatted using date-fns (yyyyMMdd HH:mm:ss format)
  • Unique IDs: Generated using uuid v4
  • Error Logging: Automatically logs errors to errLog.txt
  • Custom Logs: Can emit custom log events

Example Log Entry

20260327    10:30:45    a1b2c3d4-e5f6-4g7h-8i9j-0k1l2m3n4o5p6
Log message content

πŸ”§ Configuration

Express Logging Events

Events are emitted using Node.js EventEmitter:

myEmitter.on("log", (msg, fileName) => logEvents(msg, fileName));

Adding Routes

Edit server.js to add new routes:

case '/your-route':
    // Handle the route
    break;

πŸ“‚ File Serving

The server automatically determines content type based on file extension:

  • .html β†’ text/html
  • .json β†’ application/json
  • .css β†’ text/css
  • Images β†’ appropriate image types

πŸ› Error Handling

  • 404 Errors: Automatically served with 400 status code
  • Server Errors: Logged to logs/errLog.txt with timestamp and error details
  • Missing Files: Returns 500 status with empty response

πŸ“Š Logs

Log files are stored in the logs/ directory and automatically created as needed:

  • errLog.txt - Server errors and exceptions
  • Custom log files as created by event listeners

πŸ”„ Development Workflow

  1. Make changes to server.js or other files
  2. Nodemon automatically detects changes and restarts the server
  3. Refresh your browser to see updates
  4. Check logs for any errors or events

πŸš€ Deployment

To deploy this server:

  1. Install production dependencies:

    npm install --production
  2. Set the PORT environment variable:

    export PORT=3000  # or your desired port
  3. Start the server:

    npm start

πŸ“š Dependencies Explained

Package Purpose
date-fns Formatting and parsing dates in logs
uuid Generating unique identifiers for each log entry
nodemon (dev) Automatically restart server during development

πŸŽ“ Learning Resources

πŸ“ License

This project is open source and available for personal and educational use.

πŸ‘€ Author

Suprit Raj

  • Computer Science Engineering Student

Feel free to fork and modify this project for your learning! πŸŽ‰

About

Lightweight Node.js HTTP server with static file serving, event logging, and error tracking

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors