This project is a simple HTTP server built using Node.js. The server serves an HTML homepage, handles dynamic URLs for user-specific pages, provides an API endpoint, and returns appropriate responses for bad requests.
- Serves a static HTML homepage.
- Handles dynamic URLs to serve personalized user pages.
- Provides an API endpoint to serve JSON data.
- Returns 404 for unknown routes.
-
Clone the repository:
git clone https://github.com/theNewtonCode/Server-using-Node.js.git cd simple-http-server
-
Install the required dependencies:
npm install
-
Create the necessary directories and files if they do not exist:
mkdir -p Frontend/static Backend touch Backend/users.txt Frontend/static/index.html Frontend/static/api_data/text.txt
-
Populate
Backend/users.txt
with usernames (one per line):user1 user2
-
Add content to
Frontend/static/index.html
:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Homepage</title> </head> <body> <h1>Welcome to the Homepage</h1> </body> </html>
-
Add some text content to
Frontend/static/api_data/text.txt
.
-
Start the server:
node server.js
-
Open your browser and navigate to
http://127.0.0.1:3000
. -
To access a user-specific page, navigate to
http://127.0.0.1:3000/users/username
, whereusername
is a name listed inBackend/users.txt
. -
To access the API endpoint, navigate to
http://127.0.0.1:3000/api/data
.
Backend/
users.txt
: File containing usernames.
Frontend/
static/
index.html
: HTML file for the homepage.api_data/
text.txt
: File containing text data for the API endpoint.
server.js
: Main server file.
This project is licensed under the MIT License.