Guide

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.

Read more in Guide > Options.

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.

Read more in Guide > Options#port Required.

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

srvx tries to translate most common options to op level properties. This is only for advances usage.

server.bunServer

Access to the underlying Bun server instance when running in Bun.

Read more in bun.sh/docs/api/http.

server.denoServer

Access to the underlying Bun server instance when running in Deno.

Read more in docs.deno.com/api/deno/~/Deno.HttpServer.

server.nodeServer

Access to the underlying Node.js server instance when running in Node.js

Read more in nodejs.org/api/http.html#class-httpserver.