Overview
The Express adapter runs Courier's proxy as Express middleware: it turns the Node.js request into a Web Request, runs the core handler, and writes the Response back — streamed bodies, uploads and cancelled calls included.
Quick start
In your Express project:
npx @noverax/create-courier
It adds a router on /api/request, a typed example API, Studio and a mock — showing each change to your files before it makes it — installs the packages and type-checks the result. For a new project: npx @noverax/create-courier my-app --framework express. The steps below do the same by hand.
Install
npm install @noverax/courier-express @noverax/courier express
Usage
import express from "express"
import { registerSources } from "@noverax/courier/server"
import { courierHandler } from "@noverax/courier-express"
import { sources } from "./api/sources"
registerSources(sources)
const app = express()
app.all("/api/request", courierHandler())
app.listen(3000, () => {
console.log("Server running on http://localhost:3000")
})
courierHandler() is zero-arg — sources are read at request time from the core global registry populated by registerSources() (or by attachRuntime() auto-loading courier.config.ts in Studio-instrumented projects). The handler accepts any HTTP method. GET requests are routed to query-type endpoints, POST requests handle mutations and data payloads.
The user's session
An API that keeps the user's session describes it in defineApi, and every call a route makes — through every await — carries the session of the request being served, the cookies the session names and nothing else, once the request is served with it at hand:
import { requestContext } from "@noverax/courier-express"
app.use(requestContext())
The session cookies the API sets reach the browser on the response.
Streaming and WebSockets
import http from "node:http"
import { courierSSEHandler, courierWSAttach } from "@noverax/courier-express"
app.get("/api/sse", courierSSEHandler())
const server = http.createServer(app)
const detach = courierWSAttach(server) // answers upgrades on /api/ws
server.listen(3000)
courierWSAttach answers WebSocket upgrades on one path — /api/ws unless you pass path — and leaves every other path to the application's own WebSocket handlers. Point the client at the same path with proxyPath: { ws: "/api/ws" }. A message larger than maxMessageSize (64 KB by default) is refused with a 4413 error and the connection stays open; a frame larger than 1 MiB, or twice maxMessageSize when that is larger, closes the connection with 1009 before it is buffered. Requires the ws package.
A client whose network lets no WebSocket through — a corporate proxy, an antivirus that inspects TLS, an entry with WebSockets turned off — keeps the same connection over POST requests to that path. courierWSAttach takes them off the server ahead of Express, so none of your body parsers or routes reads them, whichever was added first. With http: false they are yours again, and the client stays on WebSocket.
Shutting down
server.close() waits for every open connection, and an SSE stream or a WebSocket stays open on its own — a stream until its lifetime cap, 30 minutes by default. Drain them first:
const proxy = courierHandler()
const sse = courierSSEHandler()
app.all("/api/request", proxy)
app.get("/api/sse", sse)
process.on("SIGTERM", async () => {
await Promise.all([proxy.shutdownSSE(), sse.shutdownSSE(), detach()])
server.close()
})
shutdownSSE() sends each stream's client a courier:shutdown notice that tells it when to reconnect, then ends the streams; sseConnections lists the open ones. detach() refuses new upgrades with 503, closes each WebSocket with 1001, telling it when to come back — which a Courier client answers by reconnecting then — and waits for them up to 5 seconds, then ends each one whose client has not answered, so it settles within that time whatever the clients and Redis do. It carries the handler's connections, stats() and destroy({ timeoutMs }), the same stop with a deadline of your own. The proxy route serves streams too, which is why it drains as well.
Limits
The proxy reads a JSON call up to its maxBodyBytes, 1 MB, and an upload (multipart/form-data) up to maxUploadBytes, 10 MB. Pass others to raise them: courierHandler({ maxUploadBytes: 50 * 1024 * 1024 }). Do not mount a body parser (express.json(), multer) on the proxy route: the handler reads the raw body itself.
How It Works
- Converts the Node.js
IncomingMessage to a Web Standard Request
- Passes it to the core
createHandler().handle()
- Converts the Web
Response back to Node.js ServerResponse
With Frontend
Pair with createProxyCaller on the client:
import { createProxyCaller } from "@noverax/courier/client"
import { routes } from "@noverax/courier/routes"
// The routes Courier's plugin builds from the endpoints you mark .expose():
// what a page may call, under the key the source has in your `sources` map.
const caller = createProxyCaller(routes.api, {
proxyPath: { http: "/api/request" },
})
const users = await caller.users.getAll()
Or with React Query:
import { createProxyQueryCaller } from "@noverax/courier-react"
import { routes } from "@noverax/courier/routes"
const q = createProxyQueryCaller(routes.api, { proxyPath: { http: "/api/request" } })
The routes come from Courier's plugin in the build that makes the page — the plugin for each build. Pages no bundler builds, as create-courier sets them up on this server, take apiRoutes from courier.routes.ts in place of routes.api, which npx @noverax/courier-cli routes writes from the same marks.
Part of Courier
License
MIT