Set up a basic HTTP server using a framework or the built-in Node.js http
module.
Configure the server to respond with "Hello, World!" for all incoming requests.
Example:
- Request:
GET /
- Response:
Hello, World!
Modify your server to extract the name from the URL path using routing mechanisms. Respond with a personalized greeting using the extracted name.
Example:
- Request:
GET /greet/John
- Response:
Hello, John!
Update your server to parse and extract query parameters from the incoming request. Include the extracted name from the query parameters in the response.
Example:
- Request:
GET /greet?name=Alice
- Response:
Hello, Alice!
Use the Node.js fs
module to read and serve static HTML files.
Create an HTML file and configure your server to serve it when a specific route is requested.
Example:
- Request:
GET /static.html
- Response: Contents of static.html file
Implement a mechanism to handle undefined routes. Respond with a 404 Not Found status code along with a custom error message.
Example:
- Request:
GET /nonexistent
- Response:
404 Not Found - Custom error message
Create a new route that returns a JSON object. Populate the JSON object with various pieces of information.
Example:
- Request:
GET /api/data
- Response:
{"name": "John", "age": 25, "city": "Example City"
Explore the Node.js response.setHeader
method to modify the response headers. Experiment with setting custom headers like Content-Type
and Cache-Control
.
Example:
- Request:
GET /
- Response:
Hello, World! Content-Type: application/json Cache-Control: no-cache
Implement a new route that triggers a redirect to another URL. Choose an appropriate HTTP status code for redirection (e.g., 301 or 302).
Example:
- Request:
GET /redirect
- Response:
302 Found - Redirecting to /new-location
Extend your server to handle different HTTP methods (GET, POST, etc.) for a specific route. Customize the server response based on the method used.
Example:
- Request:
POST /submit
- Response:
POST request received
Create a middleware function that logs information about each incoming request. Include details such as the request method, URL, and a timestamp.
Example:
- Middleware Log:
GET request at / - Timestamp: 2024-02-07 12:34:56
Update your server to handle POST requests. Extract data from the request body and respond with a message that includes the posted data.
Example:
-
Request:
POST /submit
- Body:{"username": "user123", "password": "pass123"}
-
Response:
Data received from user123
Implement a mechanism to capture the termination signal (SIGINT
). Gracefully close the server before exiting the application.
Example:
-
Console Output:
Server is shutting down gracefully... Server closed. Application exiting.