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

<CodeGroup>

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

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

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

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

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

</CodeGroup>

<read-more title="Using CLI" to="/guide/cli#usage">



</read-more>

<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)

</tip>

## 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.ts]
import { serve } from "srvx";

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

Install `srvx` as a dependency:

<pm-install name="srvx">



</pm-install>

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

</code-group>

## TypeScript

Each entry only pulls in the types of the runtime it targets. The ambient type packages below are declared as optional `peerDependencies`, so nothing is installed unless you ask for it:

<table>
<thead>
  <tr>
    <th>
      Runtime
    </th>
    
    <th>
      Types package
    </th>
    
    <th>
      Needed by
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Node.js
    </td>
    
    <td>
      <code>
        @types/node
      </code>
    </td>
    
    <td>
      <code>
        srvx/node
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Deno
    </td>
    
    <td>
      <code>
        @types/deno
      </code>
    </td>
    
    <td>
      <code>
        srvx/deno
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Bun
    </td>
    
    <td>
      <code>
        @types/bun
      </code>
    </td>
    
    <td>
      <code>
        srvx/bun
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Cloudflare Workers
    </td>
    
    <td>
      <code>
        @cloudflare/workers-types
      </code>
    </td>
    
    <td>
      <code>
        srvx/cloudflare
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      AWS Lambda
    </td>
    
    <td>
      <code>
        @types/aws-lambda
      </code>
    </td>
    
    <td>
      <code>
        srvx/aws-lambda
      </code>
    </td>
  </tr>
</tbody>
</table>

Everything else — `srvx/types`, `srvx/service-worker`, `srvx/static`, `srvx/log`, `srvx/body-limit`, `srvx/tracing`, `srvx/mtls` and `srvx/cli` — needs none of them, so a project can type check with `skipLibCheck: false`:

```ts [server.ts]
import { serve } from "srvx/node"; // only needs `@types/node`
import { staticMiddleware } from "srvx/static"; // needs nothing
import type { ServerRequest } from "srvx/types"; // needs nothing
```

Runtime specific fields such as `server.node`, `request.runtime.deno` or the `bun` option are typed as soon as the types of that runtime are loaded, and stay loosely typed (`Record<string, unknown>`) otherwise — importing `srvx/<runtime>` anywhere in your project is enough to type them everywhere.

<note>

`@types/deno` references DOM types, so pair it with `lib: ["esnext", "dom"]`.

</note>

<note>

The universal `srvx` entry covers every runtime, so it resolves all of the packages above at once. It also references `cloudflare:workers`, which `@cloudflare/workers-types` declares ambiently and therefore only resolves when that package is listed in tsconfig `types` — which in turn conflicts with `@types/deno`. Declare the module yourself to check that entry with `skipLibCheck: false`:

```ts
declare module "cloudflare:workers" {
  export const env: Record<string, unknown>;
}
```

</note>

## Starter Examples

<table>
<thead>
  <tr>
    <th>
      Example
    </th>
    
    <th>
      Source
    </th>
    
    <th>
      Try
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        aws-lambda
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/aws-lambda/" rel="nofollow">
        examples/aws-lambda
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/aws-lambda srvx-aws-lambda
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        elysia
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/elysia/" rel="nofollow">
        examples/elysia
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/elysia srvx-elysia
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        express
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/express/" rel="nofollow">
        examples/express
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/express srvx-express
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fastify
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/fastify/" rel="nofollow">
        examples/fastify
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/fastify srvx-fastify
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        h3
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/h3/" rel="nofollow">
        examples/h3
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/h3 srvx-h3
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        hello-world
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/hello-world/" rel="nofollow">
        examples/hello-world
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/hello-world srvx-hello-world
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        hono
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/hono/" rel="nofollow">
        examples/hono
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/hono srvx-hono
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        jsx
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/jsx/" rel="nofollow">
        examples/jsx
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/jsx srvx-jsx
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        node-handler
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/node-handler/" rel="nofollow">
        examples/node-handler
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/node-handler srvx-node-handler
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        service-worker
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/service-worker/" rel="nofollow">
        examples/service-worker
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/service-worker srvx-service-worker
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        streaming
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/streaming/" rel="nofollow">
        examples/streaming
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/streaming srvx-streaming
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        tracing
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/tracing/" rel="nofollow">
        examples/tracing
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/tracing srvx-tracing
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        websocket
      </code>
    </td>
    
    <td>
      <a href="https://github.com/h3js/srvx/tree/main/examples/websocket/" rel="nofollow">
        examples/websocket
      </a>
    </td>
    
    <td>
      <code>
        npx giget gh:h3js/srvx/examples/websocket srvx-websocket
      </code>
    </td>
  </tr>
</tbody>
</table>
