A lightweight Node.js HTTP server that serves static web pages with built-in event logging and error tracking capabilities.
- HTTP Server: Built with Node.js native
httpmodule - 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
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)
- 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
- Node.js (v14 or higher)
- npm (comes with Node.js)
-
Clone the Repository
git clone https://github.com/supritR21/WebServer.git cd WebServer -
Install Dependencies
npm install
npm run devUses Nodemon to automatically restart the server on file changes.
npm startRuns the server directly with Node.js.
PORT=5000 npm startDefault port is 3500.
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)
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
The logging system (logEvents.js) provides:
- Timestamps: Formatted using
date-fns(yyyyMMdd HH:mm:ss format) - Unique IDs: Generated using
uuidv4 - Error Logging: Automatically logs errors to
errLog.txt - Custom Logs: Can emit custom log events
20260327 10:30:45 a1b2c3d4-e5f6-4g7h-8i9j-0k1l2m3n4o5p6
Log message content
Events are emitted using Node.js EventEmitter:
myEmitter.on("log", (msg, fileName) => logEvents(msg, fileName));Edit server.js to add new routes:
case '/your-route':
// Handle the route
break;The server automatically determines content type based on file extension:
.htmlβtext/html.jsonβapplication/json.cssβtext/css- Images β appropriate image types
- 404 Errors: Automatically served with 400 status code
- Server Errors: Logged to
logs/errLog.txtwith timestamp and error details - Missing Files: Returns 500 status with empty response
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
- Make changes to
server.jsor other files - Nodemon automatically detects changes and restarts the server
- Refresh your browser to see updates
- Check logs for any errors or events
To deploy this server:
-
Install production dependencies:
npm install --production
-
Set the PORT environment variable:
export PORT=3000 # or your desired port
-
Start the server:
npm start
| 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 |
This project is open source and available for personal and educational use.
Suprit Raj
- Computer Science Engineering Student
Feel free to fork and modify this project for your learning! π