# 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.

```js
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 to="/guide/options">



</read-more>

### `server.url`

Get the computed server listening URL.

### `server.addr`

Listening address (hostname or ipv4/ipv6).

### `server.port`

Listening port number.

<read-more to="/guide/options#port-required">



</read-more>

### `server.waitUntil?`

Register a background task that the server should await before closing. This is the same function as [`request.waitUntil`](/guide/handler#requestwaituntil) but available at the server level for use outside of request handlers.

```js
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:**

```js
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:**

```js
// 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

<note>

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

</note>

### `server.bun.server`

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

<read-more to="https://bun.sh/docs/api/http">



</read-more>

### `server.deno.server`

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

<read-more to="https://docs.deno.com/api/deno/~/Deno.HttpServer">



</read-more>

### `server.node.server`

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

<read-more to="https://nodejs.org/api/http.html#class-httpserver">



</read-more>
