All jobs

Add Rate Limiting to a React Router App

Yes. An agent adds express-rate-limit to the Express server behind your React Router app, keeps assets outside the limit, proves the 429 with curl, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts by reading how requests reach the app, because a limiter in the wrong layer either counts every image or misses the server-rendered pages. In a project made from the node-custom-server template in remix-run/react-router-templates, server.js builds an Express 5 app (express ^5.2.1 in package.json), enables compression, disables the x-powered-by header, and then splits by NODE_ENV: in development it mounts Vite in middleware mode and loads ./server/app.ts through ssrLoadModule on every request; in production it serves build/client/assets as immutable for a year, logs with morgan("tiny"), serves the rest of build/client for an hour, and finally mounts the React Router handler from build/server/index.js. server/app.ts is the piece both modes share: it exports an Express app whose only middleware is createRequestHandler, with a getLoadContext that puts a string into a RouterContextProvider for app/routes/home.tsx to read in its loader.

That layout decides where the limiter goes. Static files are answered by server.js before the shared app, so a limiter in server/app.ts counts only document requests and the .data requests React Router sends for loaders on client-side navigation, and never the hashed assets. The agent installs express-rate-limit and registers one limiter ahead of createRequestHandler with windowMs and limit read from RATE_LIMIT_WINDOW_MS and RATE_LIMIT_MAX, standardHeaders: "draft-8" so the client gets the combined RateLimit header, and legacyHeaders: false. Because a single navigation can send a document request and a .data request for the same route, the default limit is set with that in mind and the pull request says so.

The template ships a Dockerfile on node:24-alpine and most deployments put a proxy in front of it, so the agent also sets trust proxy from a TRUST_PROXY variable in server.js before the app is mounted. Without it req.ip is the proxy's address and every visitor shares one bucket; with a careless true a client can forge X-Forwarded-For and escape the limit, which is why the value is the hop count rather than a boolean. The limiter's built-in memory store is per process, which is fine for one container; the pull request names the store option for a Redis-backed store when there are several replicas, without adding Redis to the template.

Then it proves the change in the sandbox. npm run typecheck runs react-router typegen && tsc -b, and tsconfig.node.json type-checks server.js itself with checkJs, so a wrong option name fails here. npm run build produces build/client and build/server, npm run start runs node server.js, and a loop of curl -i against / with RATE_LIMIT_MAX set low shows the RateLimit header counting down and the 429 with its Retry-After once the limit is hit, while a request for a file under /assets keeps answering 200 past the limit. The output of each of those commands is in the run's transcript.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add rate limiting to this React Router app. Read server.js, server/app.ts, app/routes.ts, app/routes/home.tsx, package.json, tsconfig.node.json and the Dockerfile first. Install express-rate-limit and register one limiter in server/app.ts ahead of createRequestHandler, so static files served by server.js are not counted but document and .data requests are. Read the window and the limit from RATE_LIMIT_WINDOW_MS and RATE_LIMIT_MAX with sensible defaults, send the draft-8 RateLimit header and no legacy headers, and set trust proxy in server.js from a TRUST_PROXY hop count so the limiter keys on the real client address behind a proxy. Run npm run typecheck, npm run build and npm run start, then show with curl that the RateLimit header counts down, that the limit answers 429 with Retry-After, and that a request under /assets is not limited. Document the three variables in the README and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch that touches package.json (the new dependency), server/app.ts (the limiter ahead of the request handler), server.js (the trust proxy line) and the README section listing RATE_LIMIT_WINDOW_MS, RATE_LIMIT_MAX and TRUST_PROXY. No route, component or build config changes, and the Dockerfile is untouched because the variables are read at start time.

The description shows the curl sequence: the RateLimit header on the first requests, the 429 answer with its Retry-After, and the 200 for an asset after the limit was reached. The typecheck and build output are in the transcript at GET /o/{org}/sessions/{id}/messages beside the commands that produced them, so a reviewer sees the limiter counted exactly the requests it was meant to count.

Before merging, a reviewer checks the default window and limit against real traffic, since a page with several loaders sends more than one request per navigation, and confirms the TRUST_PROXY hop count matches the number of proxies in front of the container. If the team runs more than one replica, one follow-up message to the same session swaps the memory store for a shared one.

Run it on every new public route

A developer who has just exposed a new route starts one run from the terminal with this prompt and the repository, then moves on while the agent reads the server, installs the limiter and runs the curl checks. The run keeps going in the cloud and the pull request arrives when it is done.

A platform team can do the same across every repository built from the template: one POST /o/{org}/sessions per repository, each with its own sandbox. GET /o/{org}/github lists the repositories the Rush GitHub App installation reaches, so a script can start a run only where server.js has no limiter yet.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The part that goes wrong with rate limiting is not the middleware call but where it sits and what it keys on. Put it in server.js before the static handlers and every stylesheet counts; leave trust proxy unset and the whole site shares the proxy's address. The agent reads the actual request path through server.js and server/app.ts and places the limiter where only page and data requests pass, then runs the built server in its own sandbox and sends the requests that show the count.

Those requests and their headers are the proof. The transcript keeps the curl output with the RateLimit header and the 429, the typecheck of server.js under checkJs, and the build, so a reviewer reads what happened rather than trusting a sentence in the description. Several repositories can be handled at once, each in its own sandbox, with one pull request each.

Frequently asked questions

Why put the limiter in server/app.ts instead of server.js?

Because server.js serves build/client/assets and build/client before it mounts the shared app, and the development branch mounts Vite's middleware first. A limiter in server/app.ts runs ahead of createRequestHandler in both modes and sees only document and .data requests, so hashed assets never consume the budget.

Does one page view count as one request?

Not always. A full page load is one document request, but a client-side navigation sends a .data request for the route's loader, and a route with several loaders can send more. The agent sets the default limit with that in mind and the README explains it, so a team can raise RATE_LIMIT_MAX rather than wonder why the count climbs faster than page views.

What happens behind a load balancer or the Docker network?

Express sees the proxy's address unless trust proxy is set, so every visitor would share one bucket. The agent sets it from TRUST_PROXY as a hop count rather than true, because trusting every X-Forwarded-For lets a client choose its own key. The pull request says what to set for one proxy and for none.

Will the limit be shared across several containers?

No. The default store is in memory and each process counts on its own. The pull request names the store option for a shared store such as Redis and leaves adding one to the team, since the template has no Redis and a wrong guess there is worse than a clear note.

More React Router jobs

The same job on other stacks

Keep reading