@cran/lib.koa.core
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Koa Server Core

CC BY-SA 4.0   NPM

Features

  • First-class integration with @koa/router
  • Prometheus telemetry by prom-client
  • Health endpoint conforming to rfc-healthcheck
  • CORS management by @koa/cors
  • Security provided by koa-helmet
  • Data compression by koa-compress
  • Default body parsing by koa-bodyparse
  • Response time tracking by koa-response-time

Installation

npm install @cran/lib.koa.core koa prom-client

Basic Usage

import { Server } from "@cran/lib.koa.core";

void (async function main ( ) {
  const server = await new Server().start();

  console.log(server.getNetwork().address());
})().catch(console.error);

Standard Usage

import { Server, Router } from "@cran/lib.koa.core";

const router = new Router("/");
// register router handlers

async function middleware ( ctx, next ) {
  // do middleware things
  await next();
}

const server = new Server()
  .use(middleware)
  .useRouter(router);

void (async function main ( ) {
  await server.start();

  console.log(server.getNetwork().address());
})().catch(console.error);

Advanced Configuration

new Server(options as Server.Options);
export namespace Server {
  export interface SystemRouterOptions<State, Context> {
    // defaults to `/-`
    prefix?: string;
    // custom health path and check function
    health?: false | string | { path: string; } & health.Options;
    // custom metrics path
    metrics?: false | string | { path: string; };
  }

  export interface Options<State, Context> {
    // useful for HTTPS cert configuration
    network?: HttpServer | HttpsServer;
    // https://www.npmjs.com/package/@koa/cors
    cors?: false | cors.Options;
    // https://www.npmjs.com/package/koa-bodyparser
    bodyParser?: false | bodyParser.Options;
    // https://www.npmjs.com/package/koa-helmet
    helmet?: false | helmet.Options;
    // https://www.npmjs.com/package/koa-response-time
    responseTime?: false | responseTime.Options;
    // https://www.npmjs.com/package/koa-compress
    compress?: false | compress.Options;
    // see above
    systemRouter?: SystemRouterOptions<State, Context>;
  }
}

Package Sidebar

Install

npm i @cran/lib.koa.core

Weekly Downloads

3

Version

0.1.1

License

MIT

Unpacked Size

29.3 kB

Total Files

27

Last publish

Collaborators

  • common-cranberry