From 880cf2b76050c594f72725c3bf110bcab46b966f Mon Sep 17 00:00:00 2001 From: Kartik Mehta Date: Wed, 30 Oct 2024 20:22:33 +0530 Subject: [PATCH] Add Middlewares Docs --- docs/middlewares/_category_.json | 2 +- docs/middlewares/body-parser.md | 27 +++++++++++++++++++ docs/middlewares/cors.md | 23 ++++++++++++++++ docs/middlewares/logger.md | 21 +++++++++++++++ docs/middlewares/rate-limit.md | 23 ++++++++++++++++ docs/middlewares/security.md | 20 ++++++++++++++ .../markdown-features.mdx | 0 7 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 docs/middlewares/body-parser.md create mode 100644 docs/middlewares/cors.md create mode 100644 docs/middlewares/logger.md create mode 100644 docs/middlewares/rate-limit.md create mode 100644 docs/middlewares/security.md rename docs/{middlewares => tutorial-extras}/markdown-features.mdx (100%) diff --git a/docs/middlewares/_category_.json b/docs/middlewares/_category_.json index 7764e09..b866c82 100644 --- a/docs/middlewares/_category_.json +++ b/docs/middlewares/_category_.json @@ -3,6 +3,6 @@ "position": 3, "link": { "type": "generated-index", - "description": "Orion Middleware Guide" + "description": "Orion uses various middlewares to handle common tasks like body parsing, CORS, logging, rate-limiting, and security. Each middleware is implemented as a class extending a standard interface to maintain consistency." } } diff --git a/docs/middlewares/body-parser.md b/docs/middlewares/body-parser.md new file mode 100644 index 0000000..107e1d6 --- /dev/null +++ b/docs/middlewares/body-parser.md @@ -0,0 +1,27 @@ +--- +sidebar_position: 1 +--- + +# Body Parser + +## Purpose +This middleware is used to handle parsing of incoming JSON and URL-encoded bodies in `Express.js`. It simplifies request body parsing for APIs. + +## Key Features +- Parses JSON bodies automatically using the `body-parser` package. +- Parses URL-encoded form data automatically. + +:::tip From the creator +Remember, if you decide you no longer need a middleware, simply delete the file. **Out of necessity, out of existence!** +::: + +## Customization +You can configure the `extended` option for URL-encoded data: +- `extended: true` allows for rich objects and arrays to be encoded. +- `extended: false` allows for simple key-value pairs. + +```bash +Location: src/middlewares/bodyParserMiddleware.js +``` + +Visit the **[official documentation](https://www.npmjs.com/package/body-parser)** for more information. diff --git a/docs/middlewares/cors.md b/docs/middlewares/cors.md new file mode 100644 index 0000000..d49adc8 --- /dev/null +++ b/docs/middlewares/cors.md @@ -0,0 +1,23 @@ +--- +sidebar_position: 2 +--- + +# CORS + +## Purpose +Enables *Cross-Origin Resource Sharing (CORS)* to allow or restrict resources on a web server depending on the originating domain. + +## Key Features +- Uses the popular `cors` package to enable CORS in the Express app. +- Ensures easy handling of cross-domain requests for APIs. + +## Customization +You can pass options to the cors function such as: +- `origin`: Configures which domains can access your API. +- `methods`: Defines allowed HTTP methods. + +```bash +Location: src/middlewares/corsMiddleware.js +``` + +Visit the **[official documentation](https://www.npmjs.com/package/cors)** for more information. diff --git a/docs/middlewares/logger.md b/docs/middlewares/logger.md new file mode 100644 index 0000000..2dea5a2 --- /dev/null +++ b/docs/middlewares/logger.md @@ -0,0 +1,21 @@ +--- +sidebar_position: 3 +--- + +# Logger - Morgan + +## Purpose +Logs incoming HTTP requests using the `morgan` library in a specified format. + +## Key Feature +- Uses the `tiny` logging format by default, which includes basic information such as HTTP method, URL, status, and response time. + +## Customization +You can switch to other predefined formats or define your own format: +- Predefined formats: `'tiny'`, `'combined'`, `'common'`, `'dev'`, etc. + +```bash +Location: src/middlewares/loggerMiddleware.js +``` + +Visit the **[official documentation](https://www.npmjs.com/package/morgan)** for more information. diff --git a/docs/middlewares/rate-limit.md b/docs/middlewares/rate-limit.md new file mode 100644 index 0000000..8af642b --- /dev/null +++ b/docs/middlewares/rate-limit.md @@ -0,0 +1,23 @@ +--- +sidebar_position: 4 +--- + +# Rate Limiting + +## Purpose +Limits the number of requests an IP can make to prevent abuse, brute-force attacks, or DoS attacks. + +## Key Features +- Uses the `express-rate-limit` library to control the rate of incoming requests. +- Default rate limit is `100` requests per 15 minutes. + +## Customization +Modify the `windowMs` and `max` options to adjust the rate limit: +- `windowMs`: The time window in milliseconds. +- `max`: The maximum number of requests an IP can make within the window. + +```bash +Location: src/middlewares/rateLimitMiddleware.js +``` + +Visit the **[official documentation](https://www.npmjs.com/package/express-rate-limit)** for more information. diff --git a/docs/middlewares/security.md b/docs/middlewares/security.md new file mode 100644 index 0000000..e88de14 --- /dev/null +++ b/docs/middlewares/security.md @@ -0,0 +1,20 @@ +--- +sidebar_position: 4 +--- + +# Security + +## Purpose +Improves the security of your `Express.js` application by setting various HTTP headers using the `helmet` library. + +## Key Feature +- Adds security-related HTTP headers such as `Strict-Transport-Security`, `X-Frame-Options`, and `Content-Security-Policy`. + +## Customization +You can configure the Helmet options to enable or disable specific protections. + +```bash +Location: src/middlewares/securityMiddleware.js +``` + +Visit the **[official documentation](https://www.npmjs.com/package/helmet)** for more information. diff --git a/docs/middlewares/markdown-features.mdx b/docs/tutorial-extras/markdown-features.mdx similarity index 100% rename from docs/middlewares/markdown-features.mdx rename to docs/tutorial-extras/markdown-features.mdx