Query Parameter Logger
In this lab, you will create a simple Node.js server with a specific route that logs query parameters into a file. The primary goal is to understand how to handle HTTP GET requests, work with query parameters, and perform file operations in Node.js.
Requirements
Objective
The main task is to create a /hello
GET route that accepts a query parameter name
. Your server should store this query parameter along with the current Unix timestamp in a file named names.txt
.
Each new entry should be stored in the format:
<timestamp>\t<name>
Constraints
- The server must run on port
1337
, you can make use ofPUBLIC_PORT
environment variable if you don't wish to hard 1337 in yourindex.js
file. - All code should be written in a single file named
index.js
. - Proper error handling should be included in your code (use
try-catch
blocks and log errors). - The environment is already set up for you; you only need to start coding.
Challenges
Challenge 1: Ensure that the names.txt
File is Created and Present
Description: Ensure that a file named names.txt
is created when the server starts. This file will be used to store the name query parameters and their corresponding timestamps.
Objectives:
- Create a file named
names.txt
if it doesn't already exist when the server starts. - Ensure that the file is created in the same directory as
index.js
.
Hints:
- Use Node.js's
fs
module to handle file operations. - Check if the file exists before attempting to create it.
- Ensure proper error handling for file operations.
Expected Outcome:
- When the server starts, the
names.txt
file should be present in the directory. - If the file already exists, it should not be overwritten.
Challenge 2: Ensure that Any New GET Request to the Server is Logged in the names.txt
File with the Timestamp and Name Value
Description: Implement the /hello
GET route such that any request with the name
query parameter is logged into the names.txt
file with the current Unix timestamp.
Objectives:
- Create a
/hello
GET route that accepts a query parametername
. - Retrieve the
name
query parameter from the request. - Get the current Unix timestamp.
- Append a new entry to the
names.txt
file in the format<timestamp>\t<name>
.
Hints:
- Use Node.js's
http
module (or a framework like Express) to create the server and define the route. - Use
Date.now()
to get the current Unix timestamp in milliseconds, then convert it to seconds by dividing by 1000. - Use Node.js's
fs
module to append data to the file. - Ensure proper error handling for both file operations and request processing.
Expected Outcome:
- When a request is made to
/hello?name=John
, an entry like1633024800\tJohn
is appended to thenames.txt
file. - Each new request should append a new line to the file with the correct timestamp and name.
Example:
-
Request:
GET /hello?name=John
-
Action:
- Retrieve the name
John
from the query parameter. - Get the current Unix timestamp, e.g.,
1633024800
. - Append the entry
1633024800\tJohn
to thenames.txt
file.
- Retrieve the name
-
File Content After Request:
1633024800 John
Notes
- Make sure to use port
1337
for your server. - Use proper error handling with
try-catch
blocks and log errors. - Keep all your code in a single file named
index.js
. - The environment is already set up for you; just start coding.
Good luck!