Middleware
Middleware is a prevalent design pattern extensively utilized in Node.js frameworks such as Express, among others. It is rooted in the Chain of Responsibility pattern, wherein one middleware calls another in a sequential manner.
Middleware consists of methods with parameters - request, response, and a next callback.
How to use in Fort.js
Fort.js provides a middleware
property, enabling the execution of various middleware functions.
Using Helmet
Let's explore how to incorporate Helmet.js.
Helmet is a middleware library that allows you to secure your API by setting various HTTP headers.
The Helmet code needs to be executed for every request, and that's why in Fort.js, we need to use the Wall components.
Let's create a wall where we will execute the helmet
middleware.
import { Wall, textResult } from "fortjs";
import * as helmet from 'helmet';
export class HelmetWall extends Wall {
async onIncoming() {
// Execute helmet middleware
const result = await this.middleWare(helmet()).execute();
}
}
To use this wall, add it to the walls
property of Fort
:
import { Fort } from "fortjs";
Fort.walls = [HelmetWall];
Similarly, other middleware can be employed in any component.
You can access the example here.