server

Run a Node server that starts, logs and shuts down cleanly.

BackendServerBuilt onH3

What it is

@kitr/server is createServer: an HTTP server that boots your providers, gives each request an id and a logger, turns errors into JSON, answers /healthz and shuts down without dropping requests. server() in kitrrc.ts makes it a platform for kitr dev, build and start.

Runs on the server only. Routes come from API routes.

Install

kitr add server api

First time? Run npm i -g @kitr/kitr once. Inside a project, the global kitr runs your project's own version.

Adds @kitr/server with h3 and srvx, an api platform, config/server.ts, server/main.ts and an empty server/routes.ts. Adds logger first if missing. Every change.

Quick start

kitr add wrote the entry. Add one route below createServer:

// server/main.ts
import { createServer } from '@kitr/server'
import { config } from '../config'
import rc from '../kitrrc'

export const server = createServer({ providers: rc.providers, config })

/** Fetch handler for runtimes that call it (Cloudflare Workers, Deno Deploy). */
export default server

server.h3.get('/api/orders/latest', () => ({ id: 'ord_1042', customer: 'Ada', total: 129 }))

Start it and ask for the order:

kitr dev api
curl -i http://localhost:3001/api/orders/latest
HTTP/1.1 200 OK
content-type: application/json;charset=UTF-8
x-request-id: 3106a213-994f-4363-b099-eb8c19dc7299

{"id":"ord_1042","customer":"Ada","total":129}

The object became JSON and the response carries a request id. For more than a couple of routes, use api. Next in the guide: Start your first server.

Built on

kitr adds the boot order, the container, the request context, request ids and logging, the error shape, body limits, health checks and graceful shutdown.

Problems it solves

Further reading