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

  1. Create the Express App:

    • Set up an Express.js application.
    • Implement the /get-time route.
  2. Implement JSON Response:

    • When a request is made to /get-time with a valid x-sent-from header, the response should be a JSON object with the current Unix timestamp.
    • Example response: { "time": 1631810967 }.
  3. Validate Custom Header:

    • Ensure that the /get-time route responds with a 400 status code if the x-sent-from header is missing or its value is not a valid string.
    • Example error response: 400 Bad Request.
  4. 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.

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 valid x-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:

  1. Implement the /get-time route in your Express app.
  2. Ensure the route responds with a JSON object containing the current Unix timestamp when a valid x-sent-from header is present.
  3. 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 the x-sent-from header, the server should respond with a 400 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 a 400 status code.
  • Example response: 400 Bad Request

Instructions:

  1. Check for the presence of the x-sent-from header in the request.
  2. If the header is missing or its value is not a valid string, respond with a 400 status code.
  3. 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:

  1. Create an in-memory data structure (e.g., an object) to track the number of requests per user.
  2. Increment the request count for each request made by a user.
  3. If the request count exceeds 10 for a user, respond with a 400 status code.
  4. 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