Guide
Server options
When starting a new server, in addition to main fetch
handler, you can provide additional options to customize listening server.
import { serve } from "srvx";
serve({
// Generic options
port: 3000,
hostname: "localhost",
// Runtime specific options
node: {},
bun: {},
deno: {},
// Main server handler
fetch: () => new Response("👋 Hello there!"),
});
There are two kind of options:
- Generic options: Top level options are intended to have exactly same functionality regardless of runtime
- Runtime specific: Allow customizing more runtime specific options
Generic options
port
The port server should be listening to.
Default is value of PORT
environment variable or 3000
.
You can set the port to
0
to use a random port.hostname
The hostname (IP or resolvable host) server listener should bound to.
When not provided, server will listen to all network interfaces by default.
If you are running a server that should not be exposed to the network, use
localhost
.reusePort
Enabling this option allows multiple processes to bind to the same port, which is useful for load balancing.
Despite Node.js built-in behavior that has
exclusive
flag enabled by default, srvx uses non-exclusive mode for consistency.xRemoteAddress
If this option is enabled, request.xRemoteAddress
will be available.
Example:
import { serve } from "srvx";
serve({
xRemoteAddress: true,
fetch: (request) =>
new Response(`Your ip address is ${request.xRemoteAddress}`),
});
In order to to provide cross-runtime consistency, a small wrapper function will be enabled for Deno and Bun runtimes if
xRemoteAddress
option is set.Runtime specific options
Node.js
Example:
import { serve } from "srvx";
serve({
node: {
maxHeadersize: 16384 * 2, // Double default
ipv6Only: true, // Disable dual-stack support
},
fetch: () => new Response("👋 Hello there!"),
});
See Node.js documentation for ServerOptions and ListenOptions for all available options.
Bun
Example:
import { serve } from "srvx";
serve({
bun: {
error(error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: { "Content-Type": "text/html" },
});
},
},
fetch: () => new Response("👋 Hello there!"),
});
Deno
Example:
import { serve } from "srvx";
serve({
deno: {
onError(error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: { "Content-Type": "text/html" },
});
},
},
fetch: () => new Response("👋 Hello there!"),
});