Skip to main content

createRequestHandler

Creates a request handler function that processes incoming HTTP requests and returns responses for your React Router application.

Signature

ServerBuild | (() => ServerBuild | Promise<ServerBuild>)
required
The server build object containing your application’s compiled routes, assets, and entry point. Can be a build object directly or a function that returns one (useful for development to get fresh builds on each request).
string
The server mode for the application. Defaults to "production". Valid values:
  • "production" - Optimized for production with minimal error details
  • "development" - Includes detailed error information and stack traces
  • "test" - Used during testing, suppresses console output

Returns

function
A request handler function that processes incoming requests.

Handler Signature

Request
required
Standard Fetch API Request object representing the incoming HTTP request
AppLoadContext
Optional context object passed to loaders and actions, useful for passing database connections, authentication state, etc.

Basic Example

filename=server.ts

With Load Context

Pass database connections or other server-side resources to your loaders and actions:
filename=server.ts
Access the context in your routes:
filename=app/routes/products.tsx

Development Mode with Fresh Builds

In development, pass a function to get fresh builds on each request:
filename=server.dev.ts

Express Adapter

filename=server.ts

Node Adapter

filename=server.ts

Cloudflare Workers

filename=worker.ts

Security Considerations

Production Mode

Always use "production" mode in production environments to:
  • Sanitize error messages sent to clients
  • Prevent exposure of stack traces
  • Optimize performance

Validate Load Context

Never pass sensitive credentials directly in the load context:

CSRF Protection

React Router includes built-in CSRF protection for non-GET requests. Configure allowed origins for cross-origin actions:
filename=app/root.tsx