Built-in Middlewares
Arkos comes with a set of built-in middlewares that handle common tasks like compression, security, and parsing. This document outlines each middleware, its purpose, and how to configure it.
The following code snippets are Arkos behind the scenes implementation of each built-in middleware.
Compression
The compression middleware reduces the size of HTTP responses sent to clients.
app.use(compression(arkosConfig?.compressionOptions));
Configuration Options:
- Pass custom compression options via
arkosConfig.compressionOptions
- Accepts all options from the compression package
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Global Rate Limiting
Protects your API from abuse by limiting the number of requests per client.
app.use(
rateLimit({
windowMs: 60 * 1000, // 1 minute
limit: 1000, // 1000 requests per windowMs
standardHeaders: "draft-7",
legacyHeaders: false,
})
);
Configuration Options:
- Override default settings via
arkosConfig.globalRequestRateLimitOptions
- Default: 1000 requests per minute
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
CORS
Configures Cross-Origin Resource Sharing policy for your API.
Configuration Options:
- Set allowed origins via
arkosConfig.cors.allowedOrigins
(array, string, or "*") - Provide custom options via
arkosConfig.cors.options
- Replace entire handler with
arkosConfig.cors.customHandler
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
JSON Body Parser
Parses incoming JSON request bodies.
app.use(express.json(arkosConfig?.jsonBodyParserOptions));
Configuration Options:
- Override with custom options via
arkosConfig.jsonBodyParserOptions
- Accepts all options from Express's json method
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Cookie Parser
Parses Cookie header and populates req.cookies
.
app.use(cookieParser(...[...(arkosConfig?.cookieParserParameters || [])]));
Configuration Options:
- Provide custom parameters via
arkosConfig.cookieParserParameters
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Query Parser
Parses query string parameters into appropriate JavaScript types.
app.use(
queryParser({
parseNull: true,
parseUndefined: true,
parseBoolean: true,
})
);
Configuration Options:
- Override default settings via
arkosConfig.queryParserOptions
- Default: parses null values, undefined values, and booleans
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Database Connection Check
Validates database connectivity before request processing.
app.use(checkDatabaseConnection);
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Request Logger
Logs incoming requests for debugging and monitoring.
app.use(handleRequestLogs);
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Global Error Handler
Captures and formats errors thrown during request processing.
app.use(errorHandler);
Customization:
- Can be disabled or replaced with your own implementation
- See Modifying Built-in Middlewares Guide
Adding Custom Middlewares
To add your own middlewares to the stack:
// src/app.ts
import arkos from "arkos";
arkos.init({
middlewares: {
additional: [myCustomMiddleware, anotherMiddleware],
},
// other configs
});
This will not override the built-in middlewares because they will be placed right after the whole built-in middleware stack, if you would like to replace or disable any built-in middleware see Modifying Built-in Middlewares Guide.
Where Custom Middlewares Are Placed
Bear in mind that these stack of middlewares will be passed right after all built-in middlewares mentioned above. If you want to put custom middlewares before all Arkos built-in configurations pass a function to configureApp
under configs to have acess to express app before any Arkos configurations, see the Acessing The Express App Guide.