A simple HTTP server built using Node.js's built-in http module. This project demonstrates basic routing and serves static HTML files. It is designed as a learning exercise to understand the fundamentals of server-side programming and routing before transitioning to more advanced frameworks like Express.js.
This screenshot illustrates the output when making a GET request to the
/aboutpath of the Node.js HTTP server using the Postman extension within VS Code. The server correctly responds with a basic HTML page, demonstrating the routing functionality for the/aboutendpoint.
- Basic HTTP server implementation.
- Serves static HTML files for different routes (
/and/about). - Handles errors for unsupported routes and methods.
- Uses Node.js's built-in
httpmodule andfs/promisesfor file handling. - Configures the server port using environment variables (
.envfile). - Lightweight and beginner-friendly.
This project was created to:
- Understand how to create a basic server using Node.js's
httpmodule. - Learn how to implement basic routing for different URL paths.
- Explore serving static HTML files instead of plain text responses.
- Practice using environment variables for configuration (e.g., port numbers).
- Build a foundation for developing more complex web applications and APIs.
-
Clone this repository:
git clone https://github.com/theekshana-nirmal/simple-node-server.git
cd simple-node-server -
Install dependencies:
npm install
-
Create a
.envfile: Create a file named.envin the root directory of the project and add the following line:PORT=3000
(You can change
3000to any port you prefer) -
Create the
publicdirectory: Inside the project root, create a folder namedpublicand add the following files:index.html: The homepage content.about.html: The content for the/aboutroute.
-
Start the server:
npm run dev
-
Access the server: Open your web browser and navigate to:
http://localhost:PORT/for the homepage.http://localhost:PORT/aboutfor the about page.
(Replace
PORTwith the value you set in your.envfile, e.g.,http://localhost:3000/)
- Understand how to create a basic server using Node.js's
httpmodule. - Learn how to implement routing to handle different URL paths.
- Explore serving static HTML files using
fs/promises. - Grasp the concept of ports and listening for incoming connections.
- Learn how to manage configuration like port numbers using environment variables (
.envfiles). - Build a foundational understanding for developing more complex web applications and APIs.
- Add support for more routes and dynamic content.
- Implement error pages for 404 and 500 errors.
- Handle different HTTP methods (e.g., POST, PUT, DELETE).
- Transition to using the Express.js framework for more robust features and easier development.
- Add middleware for logging and request parsing.
- Explore serving static assets like CSS and JavaScript files.
