Overview
All built-in middleware, one line each
Import them all from @senrijs/senri (only JWT is in @senrijs/senri/jwt). Middleware you do not use is not bundled.
import { , , , } from "@senrijs/senri";
const = ();
.(());
.(());
.("/api", ({ : ["https://example.com"] }));Security
| Middleware | What it does |
|---|---|
cors() | Allows fetch from other origins |
csrf() | Rejects POST and the like from other sites with 403 |
secureHeaders() | Adds security headers such as X-Content-Type-Options |
basicAuth() / bearerAuth() | Protects routes with Basic auth or a Bearer token |
jwt() | Verifies JWTs |
rateLimit() | Limits the number of requests in a time window |
ipRestriction() | Allows or denies by client IP |
Bodies
| Middleware | What it does |
|---|---|
bodyLimit() | Sets the body size limit |
uploadLimit() | Checks the size, number and type of uploaded files |
Responses
| Middleware | What it does |
|---|---|
timeout() | Returns 504 if the request does not finish in time |
compress() | Compresses with gzip or deflate |
etag() | Adds ETag and returns 304 when nothing changed |
cache() | Adds Cache-Control |
prettyJSON() | Formats JSON with ?pretty |
Requests
| Middleware | What it does |
|---|---|
requestId() | Gives each request an ID |
trailingSlash() | Normalizes trailing slashes |
methodOverride() | Lets HTML forms send PUT, PATCH and DELETE |
languageDetector() | Decides the language |
Operations
serveStatic() (static files), otel() (OpenTelemetry), metrics() (Prometheus) and contextStorage() are added as middleware too.
Registration order
Some middleware must be registered in a certain order.
cors()beforebasicAuth()/bearerAuth()compress()beforeetag()cache()beforeetag()prettyJSON()aftercompress()andetag()