Skip to Content
Senri

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

MiddlewareWhat 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

MiddlewareWhat it does
bodyLimit()Sets the body size limit
uploadLimit()Checks the size, number and type of uploaded files

Responses

MiddlewareWhat 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

MiddlewareWhat 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

MiddlewareWhat it does
logger()Logs one line per request
health()Answers /healthz and /readyz

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() before basicAuth() / bearerAuth()
  • compress() before etag()
  • cache() before etag()
  • prettyJSON() after compress() and etag()

On this page