Run on Node
Serve the same routes on Node's HTTP server, with dependencies supplied in-process and a graceful shutdown.
@edgetime/node serves a fetch handler on Node’s HTTP server. The routes do
not change; an entry file supplies the dependencies they ask for.
import { required } from '@edgetime/config';import { memoryRateLimiter } from '@edgetime/middleware';import { serve } from '@edgetime/node';import { type AppEnv, app } from './app.ts';
const env: AppEnv = { JWT_ISSUER: required(process.env, 'JWT_ISSUER'), JWT_AUDIENCE: required(process.env, 'JWT_AUDIENCE'), JWT_SECRET: required(process.env, 'JWT_SECRET'), clientAddress: (request) => request.headers.get('x-real-ip') ?? undefined, apiLimit: memoryRateLimiter({ limit: 300, period: 60 }), files: memoryStore(), // any object with the FileStore methods};
const server = await serve(app.fetch, { port: Number(process.env.PORT ?? 3000), env, onError: (error) => console.error(error),});
process.once('SIGTERM', async () => { const drained = await server.shutdown({ timeout: 10_000 }); process.exit(drained ? 0 : 1);});| Dependency | On Workers | On Node |
|---|---|---|
| General rate limit | Rate Limiting binding | memoryRateLimiter |
| Exact rate limit | Durable Object | memoryRateLimiter in one process, or a shared store |
| File store | R2 bucket | Your storage client behind the same methods |
| Client address | CF-Connecting-IP |
A header your proxy overwrites on every request |
In-memory implementations lose their contents on restart and are not shared between processes. Use them for a single process and for tests.
Behind a proxy
Section titled “Behind a proxy”Set origin to the public origin, such as https://api.example.com, when a proxy terminates
TLS, so request.url is correct. Read the client address only from a header the proxy overwrites
on every request; one the client controls lets callers choose their own rate-limit key.
Other servers
Section titled “Other servers”createListener(handler, { env }) returns a (request, response) listener for node:https or
the HTTP/2 compatibility API, with a drain() method for shutdown. See
Other servers.