
# Getting Started

> Build HTTP servers with web standard APIs like fetch, Request, and Response.

## Quick Start (CLI)

Create a server entry:

```js [server.ts]
export default {
  fetch(req: Request) {
    return Response.json({ hello: "world!" });
  },
};
```

Then, run the server using your favorite runtime:

```bash [npm]
npx srvx
```

```bash [pnpm]
pnpx srvx
```

```bash [yarn]
yarn dlx srvx
```

```bash [Deno]
deno -A npm:srvx
```

```bash [Bun]
bunx --bun srvx
```

:read-more{to="/guide/cli#usage" title="Using CLI"}

> [!TIP]
> You can also try examples in the [online playground](https://stackblitz.com/fork/github/h3js/srvx/tree/main/examples/stackblitz?startScript=dev&file=server.mjs)

## Quick Start (API)

Instead of using the `srvx` CLI, you can directly import the `serve` method to define a self-listening server entry.

Create a server entry:

```js [server.mjs]
import { serve } from "srvx";

const server = serve({
  fetch(request) {
    return Response.json({ hello: "world!" });
  },
});
```

Install `srvx` as a dependency:

:pm-install{name="srvx"}

Then, run the server using your favorite runtime:

::code-group

```bash [node]
node server.mjs
```

```bash [deno]
deno run --allow-env --allow-net server.mjs
```

```bash [bun]
bun run server.mjs
```

::

## TypeScript

srvx ships self-contained types: they reference no runtime type packages, so `tsc` passes with only `@types/node` (or none at all) and without `skipLibCheck`, whichever runtime you target.

Runtime objects such as `server.bun?.server`, `server.deno?.server`, `request.runtime?.cloudflare?.context` or `request.runtime?.awsLambda?.event` are described by minimal interfaces covering what srvx itself uses. They accept the official types, so passing a real `Bun.Server` or Lambda event still type checks. To reach the full surface of a runtime object, install that runtime's type package and cast:

```ts
import type { Server } from "bun";

const bunServer = server.bun?.server as Server | undefined;
bunServer?.publish("channel", "hello");
```

To type Cloudflare Workers bindings, augment `CloudflareEnv`:

The `declare module` block only augments `CloudflareEnv` in a file that is a module. In a standalone `.d.ts` with no imports or exports it would declare a whole new `srvx` module instead, so add `export {}` there.

```ts
import { serve } from "srvx";

declare module "srvx" {
  interface CloudflareEnv {
    MY_KV: KVNamespace;
  }
}

serve({
  fetch: (request) => {
    const kv = request.runtime?.cloudflare?.env.MY_KV;
    // ...
  },
});
```

## Starter Examples

<!-- automd:examples -->

| Example          | Source                                                                                     | Try                                                                  |
| ---------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------- |
| `aws-lambda`     | [examples/aws-lambda](https://github.com/h3js/srvx/tree/main/examples/aws-lambda/)         | `npx giget gh:h3js/srvx/examples/aws-lambda srvx-aws-lambda`         |
| `elysia`         | [examples/elysia](https://github.com/h3js/srvx/tree/main/examples/elysia/)                 | `npx giget gh:h3js/srvx/examples/elysia srvx-elysia`                 |
| `express`        | [examples/express](https://github.com/h3js/srvx/tree/main/examples/express/)               | `npx giget gh:h3js/srvx/examples/express srvx-express`               |
| `fastify`        | [examples/fastify](https://github.com/h3js/srvx/tree/main/examples/fastify/)               | `npx giget gh:h3js/srvx/examples/fastify srvx-fastify`               |
| `h3`             | [examples/h3](https://github.com/h3js/srvx/tree/main/examples/h3/)                         | `npx giget gh:h3js/srvx/examples/h3 srvx-h3`                         |
| `hello-world`    | [examples/hello-world](https://github.com/h3js/srvx/tree/main/examples/hello-world/)       | `npx giget gh:h3js/srvx/examples/hello-world srvx-hello-world`       |
| `hono`           | [examples/hono](https://github.com/h3js/srvx/tree/main/examples/hono/)                     | `npx giget gh:h3js/srvx/examples/hono srvx-hono`                     |
| `jsx`            | [examples/jsx](https://github.com/h3js/srvx/tree/main/examples/jsx/)                       | `npx giget gh:h3js/srvx/examples/jsx srvx-jsx`                       |
| `node-handler`   | [examples/node-handler](https://github.com/h3js/srvx/tree/main/examples/node-handler/)     | `npx giget gh:h3js/srvx/examples/node-handler srvx-node-handler`     |
| `service-worker` | [examples/service-worker](https://github.com/h3js/srvx/tree/main/examples/service-worker/) | `npx giget gh:h3js/srvx/examples/service-worker srvx-service-worker` |
| `streaming`      | [examples/streaming](https://github.com/h3js/srvx/tree/main/examples/streaming/)           | `npx giget gh:h3js/srvx/examples/streaming srvx-streaming`           |
| `tracing`        | [examples/tracing](https://github.com/h3js/srvx/tree/main/examples/tracing/)               | `npx giget gh:h3js/srvx/examples/tracing srvx-tracing`               |
| `websocket`      | [examples/websocket](https://github.com/h3js/srvx/tree/main/examples/websocket/)           | `npx giget gh:h3js/srvx/examples/websocket srvx-websocket`           |

<!-- /automd -->
