### Install and run locally Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Commands to install dependencies and start the development server. After running, visit localhost:3005/jokes/random or localhost:3005/jokes/ten in your browser. ```bash npm i && npm run dev ``` -------------------------------- ### Server startup with app.listen Source: https://github.com/15dkatz/official_joke_api Binds the app to process.env.PORT, defaulting to 3005. Logs 'listening on ' to stdout once the server starts. Runs at require time, so importing index.js starts the server immediately; the app object is not exported. ```javascript const PORT = process.env.PORT || 3005; app.listen(PORT, () => console.log(`listening on ${PORT}`)); ``` -------------------------------- ### Example types value Source: https://github.com/15dkatz/official_joke_api Example value of the `types` array at the time of writing. ```javascript ['general', 'knock-knock', 'programming', 'dad'] ``` -------------------------------- ### Example usage of randomJoke Source: https://github.com/15dkatz/official_joke_api Example of using the `randomJoke` function to get a random joke and log it to the console. ```javascript const { randomJoke } = require('./handler'); const joke = randomJoke(); console.log(joke); // { id: 132, type: 'programming', setup: 'Why do programmers prefer dark mode?...', punchline: '...' } ``` -------------------------------- ### GET /jokes/ten Source: https://github.com/15dkatz/official_joke_api Returns ten jokes. ```APIDOC ## [GET] /jokes/ten ### Description Returns ten jokes. ### Method GET ### Endpoint /jokes/ten ``` -------------------------------- ### GET /types Source: https://github.com/15dkatz/official_joke_api Returns a list of all available joke types. ```APIDOC ## [GET] /types ### Description Returns a list of all available joke types. ### Method GET ### Endpoint /types ``` -------------------------------- ### GET / Source: https://github.com/15dkatz/official_joke_api Root endpoint of the API. Returns basic information or a welcome message. ```APIDOC ## [GET] / ### Description Root endpoint of the API. Returns basic information or a welcome message. ### Method GET ### Endpoint / ``` -------------------------------- ### GET / Source: https://github.com/15dkatz/official_joke_api Returns a plain text response indicating the API is running. ```APIDOC ## [GET] / ### Description Returns a plain text response indicating the API is running. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **body** (text) - Plain text message. ``` -------------------------------- ### GET /random_ten Source: https://github.com/15dkatz/official_joke_api Returns ten random jokes. ```APIDOC ## [GET] /random_ten ### Description Returns ten random jokes. ### Method GET ### Endpoint /random_ten ``` -------------------------------- ### GET /jokes/:id - Example Requests and Responses Source: https://github.com/15dkatz/official_joke_api Shows example requests and expected responses for the GET /jokes/:id endpoint. The id is coerced with unary + before lookup, so non-numeric or non-integer values result in 404. A 404 response body is { "type": "error", "message": "joke not found" }. ```http GET /jokes/1 → 200, { "id": 1, "type": "general", "setup": "What did the fish say when it hit the wall?", "punchline": "Dam." } ``` ```http GET /jokes/999 → 404, { "type": "error", "message": "joke not found" } ``` ```http GET /jokes/abc → 404 (jokeById(+'abc') → jokeById(NaN) → undefined) ``` ```http GET /jokes/1.5 → 404 (jokeById(1.5) matches no strict-equal id) ``` -------------------------------- ### GET /jokes/random/:num examples Source: https://github.com/15dkatz/official_joke_api Examples of requests to the /jokes/random/:num endpoint. Valid num returns JSON array; non-numeric or zero returns text 'The passed path is not a number.'; num > 451 returns text 'The passed path exceeds the number of jokes (451).' All return HTTP 200. ```text GET /jokes/random/5 → 200, [ {joke}, {joke}, {joke}, {joke}, {joke} ] ``` ```text GET /jokes/random/0 → 200, text: "The passed path is not a number." ``` ```text GET /jokes/random/abc → 200, text: "The passed path is not a number." ``` ```text GET /jokes/random/9999 → 200, text: "The passed path exceeds the number of jokes (451)." ``` -------------------------------- ### GET /random_joke Source: https://github.com/15dkatz/official_joke_api Returns a single random joke. ```APIDOC ## [GET] /random_joke ### Description Returns a single random joke. ### Method GET ### Endpoint /random_joke ``` -------------------------------- ### GET /jokes/:type/ten Source: https://github.com/15dkatz/official_joke_api Returns ten jokes of a specific type. ```APIDOC ## [GET] /jokes/:type/ten ### Description Returns ten jokes of a specific type. ### Method GET ### Endpoint /jokes/:type/ten ### Parameters #### Path Parameters - **type** (string) - Required - The type of jokes to retrieve. ``` -------------------------------- ### GET /types Source: https://github.com/15dkatz/official_joke_api Returns a list of all available joke types in JSON format. ```APIDOC ## [GET] /types ### Description Returns a list of all available joke types in JSON format. ### Method GET ### Endpoint /types ### Response #### Success Response (200) - **body** (JSON string[]) - An array of joke type strings. ``` -------------------------------- ### Create Express app in index.js Source: https://github.com/15dkatz/official_joke_api Instantiates the Express application. No body-parsing middleware is registered because all routes use GET. ```javascript const app = express(); ``` -------------------------------- ### GET /jokes/ten Source: https://github.com/15dkatz/official_joke_api Returns an array of ten jokes in JSON format. ```APIDOC ## [GET] /jokes/ten ### Description Returns an array of ten jokes in JSON format. ### Method GET ### Endpoint /jokes/ten ### Response #### Success Response (200) - **body** (JSON joke[10]) - An array of ten joke objects. ``` -------------------------------- ### GET /ping Source: https://github.com/15dkatz/official_joke_api Health check endpoint. Returns a simple response to indicate the server is running. ```APIDOC ## [GET] /ping ### Description Health check endpoint. Returns a simple response to indicate the server is running. ### Method GET ### Endpoint /ping ``` -------------------------------- ### GET /jokes/random Source: https://github.com/15dkatz/official_joke_api Returns a single random joke in JSON format. ```APIDOC ## [GET] /jokes/random ### Description Returns a single random joke in JSON format. ### Method GET ### Endpoint /jokes/random ### Response #### Success Response (200) - **body** (JSON joke) - A random joke object. ``` -------------------------------- ### GET / Source: https://github.com/15dkatz/official_joke_api Returns an informational root page listing the main endpoints. This endpoint responds with plain text and a 200 status code. ```APIDOC ## [GET] / ### Description Returns an informational root page listing the main endpoints. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **Content-Type**: text/plain - **Body**: `Try /random_joke, /random_ten, /jokes/random, or /jokes/ten , /jokes/random/` #### Response Example ``` Try /random_joke, /random_ten, /jokes/random, or /jokes/ten , /jokes/random/ ``` ``` -------------------------------- ### randomSelect(number) usage example Source: https://github.com/15dkatz/official_joke_api Shows how to require and call randomSelect with a count of 5, logging the length of the returned array. ```javascript const { randomSelect } = require('./handler'); const five = randomSelect(5); // 5 distinct jokes console.log(five.length); // 5 ``` -------------------------------- ### jokeById usage example Source: https://github.com/15dkatz/official_joke_api Shows how to import and call jokeById. Demonstrates a successful lookup returning a joke object and a miss returning undefined. ```javascript const { jokeById } = require('./handler'); const joke = jokeById(1); // { id: 1, type: 'general', setup: 'What did the fish say when it hit the wall?', punchline: 'Dam.' } const missing = jokeById(100000); // undefined ``` -------------------------------- ### GET /types Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves the list of joke types available in the API. ```APIDOC ## [GET] /types ### Description Retrieves the list of joke types available in the API. ### Method GET ### Endpoint https://official-joke-api.appspot.com/types ``` -------------------------------- ### Example — drawing 3 distinct jokes Source: https://github.com/15dkatz/official_joke_api Shows how to import and call randomN. The first call returns 3 distinct joke objects; the second returns all 451 jokes in random order because n exceeds the array length. ```javascript const { randomN } = require('./handler'); const three = randomN(jokes, 3); // 3 distinct joke objects const capped = randomN(jokes, 9999); // all 451 jokes, in random order ``` -------------------------------- ### GET /jokes/random Source: https://github.com/15dkatz/official_joke_api Alias of `GET /random_joke`. Returns a single random joke object in JSON format. ```APIDOC ## [GET] /jokes/random ### Description Alias of `GET /random_joke`. Returns a single random joke object in JSON format. ### Method GET ### Endpoint /jokes/random ### Response #### Success Response (200) - **body** (application/json) - A single random joke object with fields `id`, `type`, `setup`, and `punchline`. ``` -------------------------------- ### GET /jokes/random Source: https://github.com/15dkatz/official_joke_api Returns a single random joke. This route is registered before the parameterized route, so it takes precedence. ```APIDOC ## [GET] /jokes/random ### Description Returns a single random joke. This route is registered before the parameterized route, so it takes precedence. ### Method GET ### Endpoint /jokes/random ``` -------------------------------- ### GET /jokes/:type/ten Source: https://github.com/15dkatz/official_joke_api Returns up to ten jokes of a specified type in JSON format. ```APIDOC ## [GET] /jokes/:type/ten ### Description Returns up to ten jokes of a specified type in JSON format. ### Method GET ### Endpoint /jokes/:type/ten ### Parameters #### Path Parameters - **type** (string) - Required - The type of jokes to return. ### Response #### Success Response (200) - **body** (JSON joke[≤10]) - An array of up to ten jokes of the specified type. ``` -------------------------------- ### GET /random_joke Source: https://github.com/15dkatz/official_joke_api Returns a single random joke in JSON format. ```APIDOC ## [GET] /random_joke ### Description Returns a single random joke in JSON format. ### Method GET ### Endpoint /random_joke ### Response #### Success Response (200) - **body** (JSON joke) - A random joke object. ``` -------------------------------- ### GET /jokes/:id Source: https://github.com/15dkatz/official_joke_api Returns a single joke by its ID in JSON format, or a 404 error if not found. ```APIDOC ## [GET] /jokes/:id ### Description Returns a single joke by its ID in JSON format, or a 404 error if not found. ### Method GET ### Endpoint /jokes/:id ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the joke to retrieve. ### Response #### Success Response (200) - **body** (JSON joke) - The joke object with the specified ID. #### Error Response (404) - **body** (text) - Error message indicating the joke was not found. ``` -------------------------------- ### GET /random_ten Source: https://github.com/15dkatz/official_joke_api Returns an array of ten random jokes in JSON format. ```APIDOC ## [GET] /random_ten ### Description Returns an array of ten random jokes in JSON format. ### Method GET ### Endpoint /random_ten ### Response #### Success Response (200) - **body** (JSON joke[10]) - An array of ten random joke objects. ``` -------------------------------- ### GET /ping response body Source: https://github.com/15dkatz/official_joke_api Liveness probe endpoint. Returns plain text 'pong' with HTTP 200. ```text pong ``` -------------------------------- ### GET /jokes/:id Source: https://github.com/15dkatz/official_joke_api Returns a joke by its ID. If the ID is not found, an error is forwarded to the error-handling middleware. ```APIDOC ## [GET] /jokes/:id ### Description Returns a joke by its ID. If the ID is not found, an error is forwarded to the error-handling middleware. ### Method GET ### Endpoint /jokes/:id ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the joke to retrieve. ``` -------------------------------- ### GET /jokes/:type/random Source: https://github.com/15dkatz/official_joke_api Returns a single random joke of a specified type in JSON format. ```APIDOC ## [GET] /jokes/:type/random ### Description Returns a single random joke of a specified type in JSON format. ### Method GET ### Endpoint /jokes/:type/random ### Parameters #### Path Parameters - **type** (string) - Required - The type of joke to return. ### Response #### Success Response (200) - **body** (JSON joke[1]) - An array containing a single random joke of the specified type. ``` -------------------------------- ### GET /jokes/:type/random Source: https://github.com/15dkatz/official_joke_api Returns a random joke of a specific type (e.g., programming, general). ```APIDOC ## [GET] /jokes/:type/random ### Description Returns a random joke of a specific type (e.g., programming, general). ### Method GET ### Endpoint /jokes/:type/random ### Parameters #### Path Parameters - **type** (string) - Required - The type of joke to retrieve. ``` -------------------------------- ### GET /jokes/{id} Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves a joke by its unique identifier. The id is provided as a path parameter. ```APIDOC ## [GET] /jokes/{id} ### Description Retrieves a joke by its unique identifier. The id is provided as a path parameter. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/{id} ### Parameters #### Path Parameters - **id** (string) - Required - The unique identifier of the joke. ``` -------------------------------- ### GET /ping Source: https://github.com/15dkatz/official_joke_api Health check endpoint that returns a plain text response to confirm the service is up. ```APIDOC ## [GET] /ping ### Description Health check endpoint that returns a plain text response to confirm the service is up. ### Method GET ### Endpoint /ping ### Response #### Success Response (200) - **body** (text) - Plain text message. ``` -------------------------------- ### GET /jokes/random/:num Source: https://github.com/15dkatz/official_joke_api Returns an array of a specified number of random jokes, or a text message if the number is invalid. ```APIDOC ## [GET] /jokes/random/:num ### Description Returns an array of a specified number of random jokes, or a text message if the number is invalid. ### Method GET ### Endpoint /jokes/random/:num ### Parameters #### Path Parameters - **num** (number) - Required - The number of random jokes to return. ### Response #### Success Response (200) - **body** (JSON joke[num] or text) - An array of random jokes, or a text message if the number is invalid. ``` -------------------------------- ### GET /jokes/ten Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves ten random jokes from the API. This endpoint is an alias for /random_ten. ```APIDOC ## [GET] /jokes/ten ### Description Retrieves ten random jokes from the API. This endpoint is an alias for /random_ten. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/ten ``` -------------------------------- ### GET /jokes/random Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves a random joke from the API. This endpoint is an alias for /random_joke. ```APIDOC ## [GET] /jokes/random ### Description Retrieves a random joke from the API. This endpoint is an alias for /random_joke. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/random ``` -------------------------------- ### GET /ping Source: https://github.com/15dkatz/official_joke_api Liveness probe endpoint that returns a plain text response. Used to verify the API is running and responsive. ```APIDOC ## [GET] /ping ### Description Liveness probe endpoint that returns a plain text response. Used to verify the API is running and responsive. ### Method GET ### Endpoint /ping ### Response #### Success Response (200) - **body** (text/plain) - The response body is the string `pong`. #### Response Example ``` pong ``` ``` -------------------------------- ### jokeByType(type, n) usage example Source: https://github.com/15dkatz/official_joke_api Shows how to require and call jokeByType with a type and count, including a case where the type does not exist and returns an empty array. ```javascript const { jokeByType } = require('./handler'); const n = jokeByType('dad', 2); // both dad jokes (dataset has 2) const none = jokeByType('unknown', 1); // [] — no such type ``` -------------------------------- ### GET /random_ten response body Source: https://github.com/15dkatz/official_joke_api Returns a JSON array of 10 distinct random jokes. ```json [ { "id": 12, "type": "knock-knock", "setup": "...", "punchline": "..." }, ... ] ``` -------------------------------- ### GET /random_ten Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves ten random jokes from the API. This endpoint is an alias for /jokes/ten. ```APIDOC ## [GET] /random_ten ### Description Retrieves ten random jokes from the API. This endpoint is an alias for /jokes/ten. ### Method GET ### Endpoint https://official-joke-api.appspot.com/random_ten ``` -------------------------------- ### GET /random_joke Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves a random joke from the API. This endpoint is an alias for /jokes/random. ```APIDOC ## [GET] /random_joke ### Description Retrieves a random joke from the API. This endpoint is an alias for /jokes/random. ### Method GET ### Endpoint https://official-joke-api.appspot.com/random_joke ``` -------------------------------- ### GET /jokes/:type/ten Source: https://github.com/15dkatz/official_joke_api Returns a JSON array of up to 10 distinct jokes of the given type. The type is a path parameter and is case-sensitive. ```APIDOC ## [GET] /jokes/:type/ten ### Description Returns a JSON array of up to 10 distinct jokes of the given type. The type is a path parameter and is case-sensitive. ### Method GET ### Endpoint /jokes/:type/ten ### Parameters #### Path Parameters - **type** (string) - Required - Joke category, e.g. `/jokes/programming/ten`. Case-sensitive ### Response #### Success Response (200) A JSON array of up to 10 distinct jokes of the given type. If the type has fewer than 10 jokes, the array is shorter; if the type is unknown it is empty. #### Response Example ```json [ { "id": 1, "type": "general", "setup": "...", "punchline": "..." }, ... ] ``` ``` -------------------------------- ### GET /jokes/random/{number} Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves a specified number of random jokes from the API. The number is provided as a path parameter. ```APIDOC ## [GET] /jokes/random/{number} ### Description Retrieves a specified number of random jokes from the API. The number is provided as a path parameter. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/random/{number} ### Parameters #### Path Parameters - **number** (integer) - Required - The number of random jokes to retrieve. ``` -------------------------------- ### GET /types Source: https://github.com/15dkatz/official_joke_api Returns a JSON array of the distinct joke categories in first-appearance order. This endpoint requires no authentication and always returns a 200 status code. ```APIDOC ## [GET] /types ### Description Returns a JSON array of the distinct joke categories in first-appearance order. This endpoint requires no authentication and always returns a 200 status code. ### Method GET ### Endpoint /types ### Response #### Success Response (200) - Response body is a JSON array of strings, e.g. `["general", "knock-knock", "programming", "dad"]` ### Response Example ```json ["general", "knock-knock", "programming", "dad"] ``` ``` -------------------------------- ### GET /jokes/{type}/ten Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves ten random jokes of a specific type. The type is provided as a path parameter. ```APIDOC ## [GET] /jokes/{type}/ten ### Description Retrieves ten random jokes of a specific type. The type is provided as a path parameter. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/{type}/ten ### Parameters #### Path Parameters - **type** (string) - Required - The type of joke to retrieve (e.g., programming). ``` -------------------------------- ### GET /types Source: https://github.com/15dkatz/official_joke_api Returns the list of joke categories available in the dataset. This endpoint requires no parameters. ```APIDOC ## GET /types ### Description Returns the list of joke categories available in the dataset. ### Method GET ### Endpoint /types ### Response #### Success Response (200) Returns an array of category strings. #### Response Example ```json [ "general", "programming", "knock-knock", "dad" ] ``` ``` -------------------------------- ### Joke JSON format for contribution Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Required format for adding a joke to jokes/index.json via a Pull Request. The object must include type, setup, and punchline fields. ```javascript { "type": "programming", "setup": "What's the best thing about a Boolean?", "punchline": "Even if you're wrong, you're only off by a bit." } ``` -------------------------------- ### GET /jokes/random/:num Source: https://github.com/15dkatz/official_joke_api Returns a specified number of random jokes. The `:num` parameter must be a valid number; otherwise, a 'not a number' text response is returned. ```APIDOC ## [GET] /jokes/random/:num ### Description Returns a specified number of random jokes. The `:num` parameter must be a valid number; otherwise, a 'not a number' text response is returned. ### Method GET ### Endpoint /jokes/random/:num ### Parameters #### Path Parameters - **num** (string) - Required - The number of random jokes to return. Must be a valid integer. ``` -------------------------------- ### GET /jokes/{type}/random Source: https://github.com/15dkatz/official_joke_api/blob/master/README.md Retrieves a random joke of a specific type. The type is provided as a path parameter. ```APIDOC ## [GET] /jokes/{type}/random ### Description Retrieves a random joke of a specific type. The type is provided as a path parameter. ### Method GET ### Endpoint https://official-joke-api.appspot.com/jokes/{type}/random ### Parameters #### Path Parameters - **type** (string) - Required - The type of joke to retrieve (e.g., programming). ``` -------------------------------- ### GET /jokes/:type/random Source: https://github.com/15dkatz/official_joke_api Returns a JSON array containing exactly 1 joke of the given type. The type is a path parameter and is case-sensitive. ```APIDOC ## [GET] /jokes/:type/random ### Description Returns a JSON array containing exactly 1 joke of the given type. The type is a path parameter and is case-sensitive. ### Method GET ### Endpoint /jokes/:type/random ### Parameters #### Path Parameters - **type** (string) - Required - Joke category, e.g. `/jokes/programming/random`. Case-sensitive; any string is accepted ### Response #### Success Response (200) A JSON array containing exactly 1 joke of the given type. #### Response Example ```json [ { "id": 400, "type": "programming", "setup": "...", "punchline": "..." } ] ``` ``` -------------------------------- ### GET /random_joke Source: https://github.com/15dkatz/official_joke_api Returns a single random joke object selected uniformly at random from the full dataset of 451 jokes. The response is in JSON format. ```APIDOC ## [GET] /random_joke ### Description Returns a single random joke object selected uniformly at random from the full dataset of 451 jokes. The response is in JSON format. ### Method GET ### Endpoint /random_joke ### Response #### Success Response (200) - **body** (application/json) - A single random joke object with fields `id`, `type`, `setup`, and `punchline`. #### Response Example ```json { "id": 451, "type": "general", "setup": "What do you call a bear with no teeth?", "punchline": "A gummy bear!" } ``` ``` -------------------------------- ### GET /jokes/:id Source: https://github.com/15dkatz/official_joke_api Returns a specific joke by its ID. If the joke is not found, a 404 error is returned. ```APIDOC ## GET /jokes/:id ### Description Returns a specific joke by its ID. ### Method GET ### Endpoint /jokes/:id ### Parameters #### Path Parameters - **id** (number) - Required - The ID of the joke to retrieve. ### Response #### Success Response (200) Returns a Joke object. #### Response Example ```json { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ``` #### Error Response (404) Returns an error object. #### Response Example ```json { "type": "error", "message": "Joke not found" } ``` ``` -------------------------------- ### GET /ping Source: https://github.com/15dkatz/official_joke_api Health check endpoint that returns a simple response to indicate the service is running. This endpoint requires no parameters. ```APIDOC ## GET /ping ### Description Health check endpoint that returns a simple response to indicate the service is running. ### Method GET ### Endpoint /ping ### Response #### Success Response (200) Returns a simple text response. #### Response Example ```text pong ``` ``` -------------------------------- ### Run server with PORT environment variable Source: https://github.com/15dkatz/official_joke_api Commands to run the server with a custom port (8080) or with the default port (3005). ```bash PORT=8080 node . # serve on port 8080 node . # serves on port 3005 ``` -------------------------------- ### GET /jokes/:id Source: https://github.com/15dkatz/official_joke_api Retrieves a single joke by its numeric id. The id is coerced with a unary plus before lookup, so non-numeric or non-integer values will not match any joke. ```APIDOC ## [GET] /jokes/:id ### Description Retrieves a single joke by its numeric id. The id is coerced with a unary plus before lookup, so non-numeric or non-integer values will not match any joke. ### Method GET ### Endpoint /jokes/:id ### Parameters #### Path Parameters - **id** (string) - Required - Joke id; coerced with unary `+` before lookup, e.g. `/jokes/1` ### Response #### Success Response (200) - **id** (number) - Joke id (1-based index into the dataset) - **type** (string) - Category, one of `general`, `programming`, `knock-knock`, `dad` - **setup** (string) - Setup line - **punchline** (string) - Punchline #### Error Response (404) - **type** (string) - Error type, always `error` - **message** (string) - Error message, e.g. `joke not found` #### Error Response (500) - An exception thrown inside the handler, forwarded via `next(e)` ### Request Example ``` GET /jokes/1 ``` ### Response Example ``` 200, { "id": 1, "type": "general", "setup": "What did the fish say when it hit the wall?", "punchline": "Dam." } ``` ``` GET /jokes/999 ``` ``` 404, { "type": "error", "message": "joke not found" } ``` ``` GET /jokes/abc ``` ``` 404 (jokeById(+'abc') → jokeById(NaN) → undefined) ``` ``` GET /jokes/1.5 ``` ``` 404 (jokeById(1.5) matches no strict-equal id) ``` ``` -------------------------------- ### Root endpoint response body Source: https://github.com/15dkatz/official_joke_api The response body for GET / is plain text listing the main endpoints. It is returned with status 200 and content type text/plain. ```text Try /random_joke, /random_ten, /jokes/random, or /jokes/ten , /jokes/random/ ``` -------------------------------- ### Rate limiter instantiation Source: https://github.com/15dkatz/official_joke_api Instantiates the rate limiter with no options, using default settings from the limiting-middleware package. ```javascript app.use(new LimitingMiddleware().limitByIp()); ``` -------------------------------- ### Joke object shape Source: https://github.com/15dkatz/official_joke_api Every joke payload returned by the API follows this JSON structure. The fields are id, type, setup, and punchline. ```json { "id": 132, "type": "programming", "setup": "...", "punchline": "..." } ``` -------------------------------- ### Joke object shape (derived from dataset and handler.js) Source: https://github.com/15dkatz/official_joke_api Defines the runtime shape of a joke object. Every element in jokes/index.json has type, setup, and punchline; handler.js adds an id at load time. The id is a 1-based index (1..451) assigned sequentially. ```javascript // Joke — a single entry in the dataset { id: number, // added by handler.js at load time: 1-based index (1..451) type: string, // one of the values below setup: string, // the setup line of the joke punchline: string // the punchline } ``` -------------------------------- ### GET /jokes/random/:num Source: https://github.com/15dkatz/official_joke_api Returns a specified number of random jokes from the dataset. The number of jokes is provided as a path parameter. ```APIDOC ## GET /jokes/random/:num ### Description Returns a specified number of random jokes from the dataset. ### Method GET ### Endpoint /jokes/random/:num ### Parameters #### Path Parameters - **num** (number) - Required - The number of random jokes to return. ### Response #### Success Response (200) Returns an array of Joke objects. #### Response Example ```json [ { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ] ``` ``` -------------------------------- ### GET /jokes/:type/ten Source: https://github.com/15dkatz/official_joke_api Returns ten random jokes of a specific type. The joke type is provided as a path parameter. ```APIDOC ## GET /jokes/:type/ten ### Description Returns ten random jokes of a specific type. ### Method GET ### Endpoint /jokes/:type/ten ### Parameters #### Path Parameters - **type** (string) - Required - The joke category (e.g., general, programming, knock-knock, dad). ### Response #### Success Response (200) Returns an array of Joke objects. #### Response Example ```json [ { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ] ``` ``` -------------------------------- ### GET /types - Response Body Source: https://github.com/15dkatz/official_joke_api Returns a JSON array of distinct joke categories in first-appearance order. The response type is application/json and the status code is 200. ```json ["general", "knock-knock", "programming", "dad"] ``` -------------------------------- ### GET /random_ten Source: https://github.com/15dkatz/official_joke_api Returns ten random jokes from the dataset. This endpoint is one of the main joke-serving routes and requires no parameters. ```APIDOC ## GET /random_ten ### Description Returns ten random jokes from the dataset. ### Method GET ### Endpoint /random_ten ### Response #### Success Response (200) Returns an array of Joke objects. #### Response Example ```json [ { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ] ``` ``` -------------------------------- ### GET /jokes/random Source: https://github.com/15dkatz/official_joke_api Returns a single random joke from the dataset. This endpoint is one of the main joke-serving routes and requires no parameters. ```APIDOC ## GET /jokes/random ### Description Returns a single random joke from the dataset. ### Method GET ### Endpoint /jokes/random ### Response #### Success Response (200) Returns a Joke object. #### Response Example ```json { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ``` ``` -------------------------------- ### GET /jokes/:type/random Source: https://github.com/15dkatz/official_joke_api Returns a single random joke of a specific type. The joke type is provided as a path parameter. ```APIDOC ## GET /jokes/:type/random ### Description Returns a single random joke of a specific type. ### Method GET ### Endpoint /jokes/:type/random ### Parameters #### Path Parameters - **type** (string) - Required - The joke category (e.g., general, programming, knock-knock, dad). ### Response #### Success Response (200) Returns a Joke object. #### Response Example ```json { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ``` ``` -------------------------------- ### GET /random_joke response body Source: https://github.com/15dkatz/official_joke_api Returns a single random joke object as JSON. The joke is selected uniformly at random from the full dataset of 451 jokes. ```json { "id": 451, "type": "general", "setup": "What do you call a bear with no teeth?", "punchline": "A gummy bear!" } ``` -------------------------------- ### GET /random_joke Source: https://github.com/15dkatz/official_joke_api Returns a single random joke from the dataset. This endpoint is one of the main joke-serving routes and requires no parameters. ```APIDOC ## GET /random_joke ### Description Returns a single random joke from the dataset. ### Method GET ### Endpoint /random_joke ### Response #### Success Response (200) Returns a Joke object. #### Response Example ```json { "id": 1, "type": "general", "setup": "What did the fish say when it swam into a wall?", "punchline": "Dam." } ``` ``` -------------------------------- ### randomTen() Source: https://github.com/15dkatz/official_joke_api Returns an array of 10 distinct random jokes from the full dataset. Takes no arguments. ```APIDOC ## randomTen() ### Description Returns an array of 10 distinct random jokes from the full dataset. Takes no arguments. ### Parameters *(none)* ### Response A `Joke[]` of 10 distinct random jokes (`randomN(jokes, 10)`). ``` -------------------------------- ### Google App Engine runtime declaration Source: https://github.com/15dkatz/official_joke_api Declares the Node.js runtime for Google App Engine. No other configuration sections are present. ```yaml runtime: nodejs22 ``` -------------------------------- ### Middleware chain in index.js Source: https://github.com/15dkatz/official_joke_api Applies per-IP rate limiting and CORS headers in registration order. The rate limiter runs first, then sets Access-Control-Allow-Origin to * on every response. ```javascript app.use(new LimitingMiddleware().limitByIp()); app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); next(); }); ``` -------------------------------- ### randomTen() - returns 10 random jokes Source: https://github.com/15dkatz/official_joke_api Takes no arguments and returns a Joke[] of 10 distinct random jokes by calling randomN(jokes, 10). ```javascript const randomTen = () => randomN(jokes, 10); ``` -------------------------------- ### Load jokes dataset Source: https://github.com/15dkatz/official_joke_api Loads the full joke dataset from a JSON file into a plain JavaScript array. Each joke object is extended with an `id` at load time. ```javascript const jokes = require('./jokes/index.json'); ``` -------------------------------- ### Library consumer handling for jokeById and randomSelect Source: https://github.com/15dkatz/official_joke_api Tests the undefined return from jokeById to detect missing IDs, and validates input before calling randomSelect to avoid infinite loops in randomN. ```javascript const { jokeById, randomSelect } = require('./handler'); const joke = jokeById(1); if (joke === undefined) { // no joke with this id — the /jokes/:id route turns this into a 404 } // Guard against nonsense counts before calling randomSelect: const n = randomSelect(3); // fine // randomSelect(NaN) would loop forever in randomN, so validate input first: if (Number.isInteger(n) && n > 0 && n <= count) { const picked = randomSelect(n); } ``` -------------------------------- ### jokeById error handling pattern Source: https://github.com/15dkatz/official_joke_api Shows the recommended pattern for handling a miss: check for undefined before consuming the result, e.g., to render a 404 or retry. ```javascript const joke = jokeById(42); if (!joke) { // handle miss: render a 404, retry, etc. } ``` -------------------------------- ### jokes Source: https://github.com/15dkatz/official_joke_api The full dataset as a plain JS array of joke objects, each extended with an id at load time. ```APIDOC ## jokes ### Description The full dataset as a plain JS array of joke objects, each extended with an `id` at load time. ### Properties - **jokes** (`Joke[]`) - The full dataset as a plain JS array of joke objects, each extended with an `id` at load time. ### Example ```js const jokes = require('./jokes/index.json'); ``` ``` -------------------------------- ### Module-level initialization in handler.js Source: https://github.com/15dkatz/official_joke_api Loads jokes from jokes/index.json, assigns sequential IDs, builds types list, and computes count. Mutates each joke object with an id property. ```javascript const jokes = require('./jokes/index.json'); let lastJokeId = 0; jokes.forEach(jk => jk.id = ++lastJokeId); const types = Array.from(new Set(jokes.map(joke => joke.type))); const count = Object.keys(jokes).length; ``` -------------------------------- ### Import dependencies in index.js Source: https://github.com/15dkatz/official_joke_api CommonJS imports for the Express app, rate limiter, and internal handler functions. Required at the top of index.js. ```javascript const express = require('express'); const LimitingMiddleware = require('limiting-middleware'); const { types, randomJoke, randomTen, randomSelect, jokeByType, jokeById, count } = require('./handler'); ``` -------------------------------- ### types Source: https://github.com/15dkatz/official_joke_api Distinct joke categories in first-appearance order. Used by the `/types` endpoint. ```APIDOC ## types ### Description Distinct joke categories in first-appearance order. Used by the `/types` endpoint. ### Properties - **types** (`string[]`) - Distinct joke categories in first-appearance order. ### Return Array of strings. Example value (at the time of this writing): ```js ['general', 'knock-knock', 'programming', 'dad'] ``` ``` -------------------------------- ### randomSelect(number) - returns n random jokes Source: https://github.com/15dkatz/official_joke_api Takes a count of distinct jokes to select and delegates unchanged to randomN(jokes, number). Returns a Joke[] of up to number distinct random jokes from the full dataset, fewer if number exceeds the dataset size. ```javascript const randomSelect = (number) => randomN(jokes, number); ``` -------------------------------- ### randomN(jokeArray, n) Source: https://github.com/15dkatz/official_joke_api Returns an array of n distinct jokes randomly sampled from the provided joke array. The sampling is done without replacement, and the result length is capped by the array length. ```APIDOC ## randomN(jokeArray, n) ### Description Returns an array of n distinct jokes randomly sampled from the provided joke array. The sampling is done without replacement, and the result length is capped by the array length. ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | jokeArray | `Joke[]` | yes | — | Source array to sample from. An empty array always yields `[]` (the limit is capped at the array length) | | n | `number` | yes | — | Number of distinct jokes requested. For non-integer / non-positive values the result count is governed by the loop condition `set.size < limit` (see Behavior below) | ### Return A `Joke[]` array of **distinct** jokes (no duplicate indices) of length `min(jokeArray.length, n)`. ### Behavior - `limit` is computed as `jokeArray.length < n ? jokeArray.length : n`, i.e. `limit = min(jokeArray.length, n)` for ordinary numbers (`NaN` yields `limit = NaN`). - Sampling is without replacement: indices are collected into a `Set` until the loop condition `randomIndicesSet.size < limit` is false. - Effective result sizes (non-empty array): - `n >= jokeArray.length` → the entire array (in a random order). - `n` a positive integer `< length` → exactly `n` distinct jokes. - `0 < n < 1` → `limit = n`, the loop runs once (`0 < n` is true, `1 < n` is false) → 1 joke. - `n <= 0` → `limit <= 0`, loop never runs → `[]`. - `n` fractional with `n >= 1` → loop runs until `size >= n`, i.e. `Math.ceil(n)` jokes (capped at the array length). - `n = NaN` → `limit = NaN`, the comparison `size < NaN` is never true → `[]`. - Empty array `jokeArray.length === 0`: `limit` is `min(0, n)`, i.e. `0` (or `NaN`), so the loop never runs → always `[]`. No input combination reachable through this function produces an infinite loop. ### Request Example ```js const { randomN } = require('./handler'); const three = randomN(jokes, 3); // 3 distinct joke objects const capped = randomN(jokes, 9999); // all 451 jokes, in random order ``` ``` -------------------------------- ### randomN function implementation Source: https://github.com/15dkatz/official_joke_api Returns an array of distinct jokes sampled without replacement from a jokeArray. The result length is min(jokeArray.length, n); empty arrays always yield []. Non-integer or non-positive n values produce counts governed by the loop condition set.size < limit. ```javascript const randomN = (jokeArray, n) => { const limit = jokeArray.length < n ? jokeArray.length : n; const randomIndicesSet = new Set(); while (randomIndicesSet.size < limit) { const randomIndex = Math.floor(Math.random() * jokeArray.length); if (!randomIndicesSet.has(randomIndex)) { randomIndicesSet.add(randomIndex); } } return Array.from(randomIndicesSet).map(randomIndex => { return jokeArray[randomIndex]; }); }; ``` -------------------------------- ### Select random joke Source: https://github.com/15dkatz/official_joke_api Returns a single `Joke` object selected uniformly at random from the full dataset. Takes no arguments and never throws because the dataset is non-empty. ```javascript const randomJoke = () => { return jokes[Math.floor(Math.random() * jokes.length)]; } ``` -------------------------------- ### Import handler module with require Source: https://github.com/15dkatz/official_joke_api CommonJS import for the handler module. Use require('./handler') or require('official_joke_api/handler'). ```javascript const { jokes, types, randomJoke, randomN, randomTen, randomSelect, jokeById, jokeByType, count } = require('./handler'); ``` -------------------------------- ### Count jokes in dataset Source: https://github.com/15dkatz/official_joke_api Computes the total number of jokes in the dataset. Used by the `/jokes/random/:num` route to reject `num` values exceeding the dataset size. ```javascript const count = Object.keys(jokes).length; ```