Home / File/ typescript-server.ts — fastify Source File

typescript-server.ts — fastify Source File

Architecture documentation for typescript-server.ts, a typescript file in the fastify codebase. 2 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  c45a037e_0fe4_737a_fb38_4cdbc9671f8c["typescript-server.ts"]
  2bf9986b_b7fb_0c78_f075_72fbe6f4672b["fastify.js"]
  c45a037e_0fe4_737a_fb38_4cdbc9671f8c --> 2bf9986b_b7fb_0c78_f075_72fbe6f4672b
  bd94e3ff_2705_51a0_bac0_884f2c38b615["node:http"]
  c45a037e_0fe4_737a_fb38_4cdbc9671f8c --> bd94e3ff_2705_51a0_bac0_884f2c38b615
  style c45a037e_0fe4_737a_fb38_4cdbc9671f8c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Most type annotations in this file are not strictly necessary but are
 * included for this example.
 *
 * To run this example execute the following commands to install typescript,
 * transpile the code, and start the server:
 *
 * npm i -g typescript
 * tsc examples/typescript-server.ts --target es6 --module commonjs
 * node examples/typescript-server.js
 */

import fastify, { FastifyInstance, RouteShorthandOptions } from '../fastify'
import { Server, IncomingMessage, ServerResponse } from 'node:http'

// Create an http server. We pass the relevant typings for our http version used.
// By passing types we get correctly typed access to the underlying http objects in routes.
// If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
const server: FastifyInstance<
  Server,
  IncomingMessage,
  ServerResponse
> = fastify({ logger: true })

// Define interfaces for our request. We can create these automatically
// off our JSON Schema files (See TypeScript.md) but for the purpose of this
// example we manually define them.
interface PingQuerystring {
  foo?: number;
}

interface PingParams {
  bar?: string;
}

interface PingHeaders {
  a?: string;
}

interface PingBody {
  baz?: string;
}

// Define our route options with schema validation
const opts: RouteShorthandOptions = {
  schema: {
    body: {
      type: 'object',
      properties: {
        pong: {
          type: 'string'
        }
      }
    }
  }
}

// Add our route handler with correct types
server.post<{
  Querystring: PingQuerystring;
  Params: PingParams;
  Headers: PingHeaders;
  Body: PingBody;
}>('/ping/:bar', opts, (request, reply) => {
  console.log(request.query) // this is of type `PingQuerystring`
  console.log(request.params) // this is of type `PingParams`
  console.log(request.headers) // this is of type `PingHeaders`
  console.log(request.body) // this is of type `PingBody`
  reply.code(200).send({ pong: 'it worked!' })
})

// Start your server
server.listen({ port: 8080 }, (err, address) => {
  if (err) {
    console.error(err)
    process.exit(1)
  }
  console.log(`server listening on ${address}`)
})

Domain

Dependencies

Frequently Asked Questions

What does typescript-server.ts do?
typescript-server.ts is a source file in the fastify codebase, written in typescript. It belongs to the CoreKernel domain.
What does typescript-server.ts depend on?
typescript-server.ts imports 2 module(s): fastify.js, node:http.
Where is typescript-server.ts in the architecture?
typescript-server.ts is located at examples/typescript-server.ts (domain: CoreKernel, directory: examples).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free