Skip to Content
ZarexDocs

Express

Protect an Express application with the @zarex/express middleware.

Installation

npm install @zarex/express
.env
ZAREX_KEY=zarex_test_...

Node.js doesn't load this file by itself: start the application with node --env-file=.env server.ts (Node 24 runs TypeScript directly when your package.json contains "type": "module"), or use the dotenv package.

Protect the application

server.ts
import express from "express";
import { zarex, shield, rateLimit } from "@zarex/express";

const app = express();

app.use(
  zarex({
    key: process.env.ZAREX_KEY!,
    rules: [shield(), rateLimit({ max: 100, window: "1m" })],
  })
);

app.get("/", (req, res) => res.send("ok"));
app.listen(3000);

The middleware can also protect a single route: app.post("/login", zarex({ ... }), handler).

A denied request automatically receives 403, or 429 with Retry-After.

The visitor's IP address

Zarex uses req.ip, computed by Express according to the trust proxy setting. Behind a proxy (load balancer, nginx, Cloudflare…), set it to match your actual infrastructure:

app.set("trust proxy", 1); // one trusted proxy in front of Express

Without this setting behind a real proxy, all your visitors seem to come from the same address, and per-IP rate limiting no longer works.

Inspect request bodies

Since 0.2.0, the body is inspected by default (in memory, never stored; inspectBody: false to turn it off). Place the middleware after express.json(), otherwise there is no body to read yet:

app.use(express.json());
app.use(zarex({ key: process.env.ZAREX_KEY!, rules: [shield()] }));

Identifiers and form data

call provides, for each request, what Zarex cannot guess: the user's identifier, the email or phone number to validate, the text to inspect.

app.use(authMiddleware); // before Zarex, so that req.user exists

app.post(
  "/signup",
  zarex({
    key: process.env.ZAREX_KEY!,
    rules: [rateLimit({ max: 5, window: "10m", characteristics: ["userId"] }), emailValidation()],
    call: (req) => ({
      characteristics: { userId: req.user?.id },
      email: req.body.email,
    }),
  }),
  handler
);

Identifiers are hashed (SHA-256) before being sent. An error in call is ignored: the request continues.

Read the decision

The decision is available in req.zarex, whether the request was allowed or not:

app.get("/", (req, res) => {
  if (req.zarex?.degraded) console.warn("Zarex could not decide");
  res.send("ok");
});

See the decision for all its fields.

Customize the denial

zarex({
  key: process.env.ZAREX_KEY!,
  rules: [shield()],
  onDeny: (req, res, decision) => {
    res.status(403).json({ error: "Request denied", id: decision.id });
  },
});

Security headers and responses

OptionEffect
secureHeaders: trueAdds HSTS, X-Content-Type-Options, X-Frame-Options, Referrer-Policy… to every response, and removes X-Powered-By.
responseGuard: trueWatches your application's responses (res.send, res.json) and masks card numbers, API keys or tokens that would end up there by mistake. Everything happens on your server.

responseGuard does not cover res.write() / res.end() called directly, nor streams.

On this page