Server Instance
Control srvx server lifecycle.
When calling serve to start a server, a server instance will be immediately returned in order to control the server instance.
import { serve } from "srvx";
const server = serve({
fetch(request) {
return new Response(`🔥 Server is powered by ${server.runtime}.`);
},
});
await server.ready();
console.log(`🚀 Server is ready at ${server.url}`);
// When server is no longer needed
// await server.close(true /* closeActiveConnections */)#Server Properties
#server.options
Access to the sever options set during initialization.
#server.url
Get the computed server listening URL.
#server.waitUntil?
Register a background task that the server should await before closing. This is the same function as request.waitUntil but available at the server level for use outside of request handlers.
import { serve } from "srvx";
const server = serve({
fetch: (request) => new Response("OK"),
});
const promise = fetch("https://telemetry.example.com", {
method: "POST",
body: JSON.stringify({ event: "server_started" }),
});
server.waitUntil?.(promise);#Server Methods
#server.ready()
Returns a promise that will be resolved when server is listening to the port and ready to accept connections.
Example:
await server.ready();#server.close(closeActiveConnections?)
Stop listening to prevent new connections from being accepted.
By default, calling close does not cancel in-flight requests or websockets. That means it may take some time before all network activity stops.
If closeActiveConnections is set to true, it will immediately terminate in-flight requests, websockets, and stop accepting new connections.
Example:
// Stop accepting new requests
await server.close();
// Stop accepting new requests and cancel all current connections
await server.close(true);#Access to the Underlying Server
#server.bun.server
Access to the underlying Bun server instance when running in Bun.
#server.deno.server
Access to the underlying Deno server instance when running in Deno.
#server.node.server
Access to the underlying Node.js server instance when running in Node.js