# The server

> How procedures, routers, context and the handler fit together, and where each page of this section comes in.

A server is a few plain values. `initTrpc()` makes the root `t` from the context type, `t.procedure` builds procedures, a router is an object of procedures, and `createFetchHandler` serves the router over HTTP, SSE and WebSockets. This is the blog app that most code blocks in these docs build on:

```ts title="server.ts"
/**
 * The app most pages' code blocks build on: a blog's router, its handler
 * and its context. Blocks import it as `./server.ts` behind a cut.
 */
import { createFetchHandler, error, initTrpc } from '@trpc12/trpc/server';
import { z } from 'zod';

import { db, type User } from './db.ts';

export { db, type Post, type User } from './db.ts';

export interface Context {
  user: User | null;
}

export const t = initTrpc<{ ctx: Context }>();

export const authed = t.procedure.use((opts) =>
  opts.ctx.user
    ? opts.next({ ctx: { user: opts.ctx.user } })
    : error({ code: 'UNAUTHORIZED' }),
);

export const PostSchema = z.object({
  id: z.string(),
  title: z.string(),
  author: z.string().nullable(),
  createdAt: z.date(),
});

export const appRouter = {
  post: {
    byId: t.procedure
      .input(z.object({ id: z.string() }))
      .query(
        (opts) =>
          db.post.find(opts.input.id) ??
          error({ code: 'NOT_FOUND', data: { id: opts.input.id } }),
      ),
    list: t.procedure.query(() => db.post.list()),
    create: authed
      .input(z.object({ title: z.string().min(1) }))
      .mutation((opts) =>
        db.post.create({ ...opts.input, author: opts.ctx.user.name }),
      ),
  },
};

export type AppRouter = typeof appRouter;

export const handler = createFetchHandler({
  router: appRouter,
  createContext: () => ({ user: null }),
});
```

## Building procedures

- [Procedures](https://v12.trpc.io/v12/docs/server/procedures.md): queries, mutations and subscriptions, with validated input and output.
- [Routers](https://v12.trpc.io/v12/docs/server/routers.md): objects of procedures, merged and split into lazy subtrees.
- [Context](https://v12.trpc.io/v12/docs/server/context.md): what every call gets as `ctx`, from the request or from middleware.
- [Middleware](https://v12.trpc.io/v12/docs/server/middleware.md): base procedures that add to the context, replace the input or stop a call. [Built-in middleware](https://v12.trpc.io/v12/docs/middleware/overview.md) covers lifecycle hooks.

## What a procedure returns

- [Errors](https://v12.trpc.io/v12/docs/server/errors.md): one error class, typed on the client from what resolvers return.
- [Streaming](https://v12.trpc.io/v12/docs/server/streaming.md): async generators, Effect streams and nested promises.
- [Files](https://v12.trpc.io/v12/docs/server/files.md): `File` and `Blob` values in inputs and outputs.
- [Responses](https://v12.trpc.io/v12/docs/server/responses.md): headers, status, or a `Response` of your own.
- [Serializers](https://v12.trpc.io/v12/docs/server/serializers.md): plain JSON by default, or types like `Date` and `Map` that survive the wire.

## Growing an app

- [Effect](https://v12.trpc.io/v12/docs/server/effect.md): resolvers that return Effects, with services from the handler's layer.
- [Extensions](https://v12.trpc.io/v12/docs/server/extensions.md): packages that add middleware, context, meta and errors to every procedure.
- [Server-side calls](https://v12.trpc.io/v12/docs/server/server-side-calls.md): call procedures in process, for tests and server code.

The handler runs on every runtime: [Adapters](https://v12.trpc.io/v12/docs/adapters/overview.md) has one page per runtime.
