A collection of utility middlewares for the ThanhhoaJS framework.
To install the package, run the following command:
bun install @thanhhoajs/utilsThis package provides several middleware utilities that can be used with the ThanhhoaJS framework. Below is a brief overview of each middleware and how to use them.
The cacheMiddleware provides caching for HTTP GET requests using an LRU cache. It supports ETag-based caching and returns a 304 Not Modified response if the client's cached version is still valid.
import { cacheMiddleware } from "@thanhhoajs/utils";
app.use(cacheMiddleware());The compressionMiddleware compresses HTTP response bodies using gzip compression. It checks the Accept-Encoding header to determine if the client supports gzip compression.
import { compressionMiddleware } from "@thanhhoajs/utils";
app.use(compressionMiddleware({}));The corsMiddleware handles Cross-Origin Resource Sharing (CORS) for HTTP requests. It allows you to configure CORS headers, including origin, methods, and allowed headers.
import { corsMiddleware } from "@thanhhoajs/utils";
app.use(
corsMiddleware({
origin: "https://example.com",
credentials: true,
})
);The helmetMiddleware sets various HTTP security headers to help protect your application from common web vulnerabilities. It includes options for Content Security Policy (CSP), HSTS, X-Frame-Options, and more.
import { helmetMiddleware } from "@thanhhoajs/utils";
app.use(
helmetMiddleware({
contentSecurityPolicy: {
"default-src": ["'self'"],
"script-src": ["'self'", "'unsafe-inline'"],
},
})
);The rateLimiterMiddleware provides rate limiting for incoming requests. It can use either an in-memory store or Redis for distributed rate limiting.
import { rateLimiterMiddleware } from "@thanhhoajs/utils";
app.use(
await rateLimiterMiddleware({
windowMs: 60000, // 1 minute
maxRequests: 100, // Limit each IP to 100 requests per windowMs
redis: {
enabled: true,
url: "redis://localhost:6379",
},
})
);Each middleware can be configured with specific options to tailor its behavior to your application's needs. Refer to the individual middleware documentation for detailed configuration options.
Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
Nguyen Nhu Khanh kwalker.nnk@gmail.com
This project is licensed under the MIT License. See the LICENSE file for details.