Server instance
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.
Example:
import { serve } from "srvx";
const server = serve({
port: 3000,
fetch: (request) => new Response("👋 Hello there!"),
});
await server.ready();
// Prints "Server ready at http://localhost:3000/"
console.log(`🚀 Server ready at ${server.url}`);
server.addr
Listening address (hostname or ipv4/ipv6).
server.port
Listening port number.
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.bunServer
Access to the underlying Bun server instance when running in Bun.
server.denoServer
Access to the underlying Bun server instance when running in Deno.
server.nodeServer
Access to the underlying Node.js server instance when running in Node.js