Hang on...
Request Threshold Blocker
Welcome to the Express.js coding lab! In this lab, your task is to implement a simple Express.js application that includes a /get-time
route. This route will return the current time in Unix timestamp format, but with some specific requirements and constraints.
Objectives
- Implement a route
/get-time
that returns the current time in Unix timestamp format. - Ensure the route only responds if a custom request header
x-sent-from
is present and has a valid string value. - Implement rate limiting to restrict the number of requests per user to a maximum of 10 requests.
Instructions
-
Create the Express App:
- Set up an Express.js application.
- Implement the
/get-time
route.
-
Implement JSON Response:
- When a request is made to
/get-time
with a validx-sent-from
header, the response should be a JSON object with the current Unix timestamp. - Example response:
{ "time": 1631810967 }
.
- When a request is made to
-
Validate Custom Header:
- Ensure that the
/get-time
route responds with a400
status code if thex-sent-from
header is missing or its value is not a valid string. - Example error response:
400 Bad Request
.
- Ensure that the
-
Implement Rate Limiting:
- Track the number of requests made by each user based on the
x-sent-from
header. - Allow a maximum of 10 requests per user. If a user exceeds this limit, respond with a
400
status code. - Example error response after exceeding the limit:
400 Bad Request
.
- Track the number of requests made by each user based on the
Constraints
- Use port number 1337, but you can reference it using
process.env.PUBLIC_PORT
. - Always use
process.env.PUBLIC_HOSTNAME
to get the URL of the backend server. - Utilize the
axios
library for making network requests. - Log errors using
console.error
in try-catch blocks. - Implement the solution in a single file for simplicity.
- Use ESM syntax exclusively (no CommonJS).
Example
Here’s an example of how you might structure your response:
{ "time": 1631810967 }
Challenges Information
Challenge 1: /get-time
returns proper JSON response
Objective: Ensure that the /get-time
route returns the current time in Unix timestamp format.
Details:
- When a request is made to
/get-time
with a validx-sent-from
header, the response should include a JSON object. - The JSON object should have a single key
time
with the value being the current Unix timestamp. - Example response:
{ "time": 1631810967 }
Instructions:
- Implement the
/get-time
route in your Express app. - Ensure the route responds with a JSON object containing the current Unix timestamp when a valid
x-sent-from
header is present. - Use
res.json()
to send the JSON response.
Challenge 2: /get-time
returns 400 in absence of custom request header
Objective: Validate that the route correctly handles missing or invalid custom request headers.
Details:
- When a request is made to
/get-time
without thex-sent-from
header, the server should respond with a400
status code indicating a bad request. - Similarly, if the
x-sent-from
header is present but its value is not a valid string, the server should also respond with a400
status code. - Example response:
400 Bad Request
Instructions:
- Check for the presence of the
x-sent-from
header in the request. - If the header is missing or its value is not a valid string, respond with a
400
status code. - Use
res.status(400).send()
to send the error response.
Challenge 3: /get-time
returns 400 when exceeds 10 requests per id threshold
Objective: Implement and verify rate limiting based on the x-sent-from
header.
Details:
- Each unique value of the
x-sent-from
header represents a unique user. - Track the number of requests made by each user.
- Allow up to 10 requests per user. If a user exceeds this limit, the server should respond with a
400
status code. - Example response after exceeding limit:
400 Bad Request
Instructions:
- Create an in-memory data structure (e.g., an object) to track the number of requests per user.
- Increment the request count for each request made by a user.
- If the request count exceeds 10 for a user, respond with a
400
status code. - Use
res.status(400).send()
to send the error response.
Adding your container request
Getting your dedicated container
Connecting to your container
Setting up your editor
Finalizing your playground
TerminalEditorBrowser