Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@alwatr/nano-server

Alwatr252MIT1.2.7TypeScript support: included

Elegant powerful nodejs server for nanoservice use cases, written in tiny TypeScript module.

server, nanoservice, api, typescript, esm, alwatr

readme

Alwatr NanoServer - @alwatr/nano-server

Elegant powerful nodejs server for nanoservice use cases, written in tiny TypeScript module.

Example usage

import {type AlwatrConnection, AlwatrNanoServer} from 'https://esm.run/@alwatr/nano-server';

const nanoServer = new AlwatrNanoServer();

nanoServer.route('GET', '/', async (connection: AlwatrConnection) => {
  connection.reply({
    ok: true,
    data: {
      app: 'Alwatr Nanoservice Starter Kit',
      message: 'Hello ;)',
    },
  });
});

API

AlwatrNanoServer(config?: Partial<Config>)

Create a server for nanoservice use cases.

Example:

import {AlwatrNanoServer} from 'https://esm.run/@alwatr/nano-server';
const nanoServer = new AlwatrNanoServer();

nanoserver.close()

Stops the HTTP server from accepting new connections.

route(method: Methods, route: 'all' |/${string},middleware: (connection: AlwatrConnection) => void)

Refers to how an application’s endpoints (URIs) respond to client requests.

Example:

nanoServer.route('GET', '/', middleware);

AlwatrConnection(incomingMessage: IncomingMessage, serverResponse: ServerResponse)

????

async function middleware(connection: AlwatrConnection) => {
connection.reply({
  ok: true,
  data: {
   app: 'Alwatr Nanoservice Starter Kit',
   message: 'Hello ;)',
  },
 });
});

connection.url: URL

Request URL.

`connection.method: "ALL" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "TRACE" | "OPTIONS" | "PATCH"

`

Request method.

connection.getBody(): Promise<string | null>

Get request body for POST, PUT and POST methods..

Example:

const body = await connection.getBody();

connection.getToken(): string | null

Get the token placed in the request header.

connection.reply(content: ReplyContent)

Example:

nanoServer.route('GET', '/', async (connection) => {
  connection.reply({
    ok: true,
    data: {
      app: 'Alwatr Nanoservice Starter Kit',
      message: 'Hello ;)',
    },
  });
});

connection.requireJsonBody()

Parse request body.

Example:

const bodyData = await connection.requireJsonBody();
if (bodyData == null) return;

requireToken(validator: ((token: string) => boolean) | Array<string> | string): string | null

Parse and validate request token. Returns request token.

Example:

const token = connection.requireToken((token) => token.length > 12);
if (token == null) return;

requireQueryParams<T>(params: Record<string, ParamType>): T | null

Parse and validate query params. Returns query params object.

Example:

const params = connection.requireQueryParams<{id: string}>({id: 'string'});
if (params == null) return;
console.log(params.id);

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.2.7 (2023-12-19)

Note: Version bump only for package @alwatr/nano-server

1.2.6 (2023-12-19)

Note: Version bump only for package @alwatr/nano-server

1.2.5 (2023-12-11)

Note: Version bump only for package @alwatr/nano-server

1.2.4 (2023-12-11)

Note: Version bump only for package @alwatr/nano-server

1.2.3 (2023-12-09)

Note: Version bump only for package @alwatr/nano-server

1.2.2 (2023-11-29)

Note: Version bump only for package @alwatr/nano-server

1.2.1 (2023-11-23)

Note: Version bump only for package @alwatr/nano-server

1.2.0 (2023-11-14)

Features

1.1.12 (2023-11-08)

Bug Fixes

  • move repo urls (719aa3e) by @AliMD

1.1.11 (2023-11-01)

Note: Version bump only for package @alwatr/nano-server

1.1.10 (2023-10-23)

Note: Version bump only for package @alwatr/nano-server

1.1.9 (2023-10-23)

Note: Version bump only for package @alwatr/nano-server

1.1.8 (2023-10-23)

Bug Fixes

  • nano-server: compatible with new logger api (ecace8b) by @AliMD

1.1.7 (2023-10-23)

Note: Version bump only for package @alwatr/nano-server

1.1.6 (2023-10-23)

Note: Version bump only for package @alwatr/nano-server

1.1.5 (2023-09-19)

Bug Fixes

  • package repo urls (466cbe9) by @AliMD

1.1.4 (2023-09-19)

Note: Version bump only for package @alwatr/nano-server

1.1.3 (2023-09-19)

Note: Version bump only for package @alwatr/nano-server

1.1.2 (2023-09-12)

Note: Version bump only for package @alwatr/nano-server

1.1.1 (2023-09-12)

Note: Version bump only for package @alwatr/nano-server

1.1.0 (2023-09-12)

1.0.0 (2023-06-14)

0.32.0 (2023-05-27)

Bug Fixes

0.31.0 (2023-05-08)

Bug Fixes

  • nano-server: path prefix (83995f7)
  • nano-server: prefixPattern, logger (f9d203e)
  • new logger api (9d83a7d)

Features

  • nano-server: add getUserAuth (615266f)
  • nano-server: prefix pattern (ad5e1b5)

0.30.0 (2023-03-06)

Features

  • nano-server: getRemoteAddress (eff82cb)
  • nano-server: requireClientId (fccd734)

0.29.0 (2023-02-10)

Bug Fixes

  • nano-server: compatible with ParamValueType (325d896)
  • nano-server: null parsedParams (bb969b2)
  • nano-server: type (2586061)

Features

  • nano-server: add client-id to IncomingHttpHeaders type (a4a86c7)
  • nano-service: api prefix support (21960dc)

0.28.0 (2023-01-20)

Bug Fixes

  • nano-server: math references (e46071a)
  • type: revert paramkey (f1ad066)

0.27.0 (2022-12-29)

Bug Fixes

0.26.0 (2022-12-22)

Bug Fixes

  • inline home route (ce978f6)
  • nano-server: check method on getBody (9aa9857)
  • nano-server: handle utf8 reply issue (33aa238)
  • nano-server: remove return null on requireToken (3c6779f)
  • nano-server: route generic (94e181b)
  • nano-server: update docs and return type (df949c1)
  • set correct path (d01ce6f)
  • tsconfig (e96dcd3)

Features

  • improve accident debugging (d697e6c)
  • improve error debugging (1fba504)
  • nano-server: clear debug info from client for security reasons. (5814af2)
  • nano-server: maybe promise type (d782761)
  • nano-server: reply throw on route (083dfa5)
  • nano-server: routeMiddleware type (a05b1a2)

1.0.0 (2023-06-14)

Note: Version bump only for package @alwatr/nano-server

0.32.0 (2023-05-27)

Bug Fixes

0.31.0 (2023-05-08)

Bug Fixes

  • nano-server: path prefix (83995f7)
  • nano-server: prefixPattern, logger (f9d203e)
  • new logger api (9d83a7d)

Features

  • nano-server: add getUserAuth (615266f)
  • nano-server: prefix pattern (ad5e1b5)

0.30.0 (2023-03-06)

Features

  • nano-server: getRemoteAddress (eff82cb)
  • nano-server: requireClientId (fccd734)

0.29.0 (2023-02-10)

Bug Fixes

  • nano-server: compatible with ParamValueType (325d896)
  • nano-server: null parsedParams (bb969b2)
  • nano-server: type (2586061)

Features

  • nano-server: add client-id to IncomingHttpHeaders type (a4a86c7)
  • nano-service: api prefix support (21960dc)

0.28.0 (2023-01-20)

Bug Fixes

0.27.0 (2022-12-29)

Note: Version bump only for package @alwatr/nano-server

0.26.0 (2022-12-22)

Bug Fixes

  • inline home route (ce978f6)
  • nano-server: check method on getBody (9aa9857)
  • nano-server: handle utf8 reply issue (33aa238)
  • nano-server: remove return null on requireToken (3c6779f)
  • nano-server: route generic (94e181b)
  • nano-server: update docs and return type (df949c1)
  • set correct path (d01ce6f)
  • tsconfig (e96dcd3)

Features

  • improve accident debugging (d697e6c)
  • improve error debugging (1fba504)
  • nano-server: clear debug info from client for security reasons. (5814af2)
  • nano-server: maybe promise type (d782761)
  • nano-server: reply throw on route (083dfa5)
  • nano-server: routeMiddleware type (a05b1a2)

0.25.0 (2022-12-07)

Note: Version bump only for package @alwatr/nano-server

0.24.1 (2022-12-01)

Note: Version bump only for package @alwatr/nano-server

0.24.0 (2022-11-28)

Bug Fixes

  • demo: update types (010b123)
  • nano-server: _sanitizeParam types (bd4f39d)
  • nano-server: deps (ea0ceba)
  • nano-server: handle preflight request (ab575cd)
  • nano-server: use config for cors (947dad9)
  • use ~ for package version (4e027ff)

0.23.0 (2022-11-23)

Bug Fixes

0.22.1 (2022-11-21)

Bug Fixes

  • add rest client extension to recommendation (33a4a25)

0.22.0 (2022-11-20)

Bug Fixes

  • nano-server: method all (b20afa1)
  • nano-server: no-non-null-assertion (ad5588c)
  • nano-server: parse body on PATCH method (cba790f)
  • nano-server: remove ALL from Methods type (1f67c07)
  • nano-server: review require... methods and fix issues (a7e4bae)
  • services/storage: remove home route (5b4160d)

Features

  • nano-server: add _sanitizeParam (46edbc3)
  • nano-server: keep alive performance and options (5ed78b6)
  • nano-server: parse and validate query params in AlwatrConnection (1eadd68)
  • nano-server: validate content type header in requireJsonBody (d99eb74)
  • services/storage: add remove route (d4452cd)
  • services/storage: add requireToken to AlwatrConnection (8073773)

Performance Improvements

  • nano-server: faster log (8ff716e)

0.21.0 (2022-11-13)

Note: Version bump only for package @alwatr/nano-server

0.20.0 (2022-11-05)

Note: Version bump only for package @alwatr/nano-server

0.19.0 (2022-11-01)

Note: Version bump only for package @alwatr/nano-server

0.18.0 (2022-10-22)

Note: Version bump only for package @alwatr/nano-server

0.17.0 (2022-10-21)

Note: Version bump only for package @alwatr/nano-server

0.16.0 (2022-09-08)

Note: Version bump only for package @alwatr/nano-server

0.15.0 (2022-09-01)

Note: Version bump only for package @alwatr/nano-server

0.14.0 (2022-08-19)

Note: Version bump only for package @alwatr/nano-server

0.13.0 (2022-08-06)

Bug Fixes

  • nano-server: incomingMessage body (0172390)
  • nano-server: host log (c0c0f97)
  • nano-server: logs (72cb160)

Features

  • nano-server: add token to connection (8677999)
  • nano-server: refactor, add seperate config, cache all routes (2cdd203)

0.12.0 (2022-07-22)

Bug Fixes

  • nano-server: logger scope (73dbf02)

Features

  • nano-server: make statusCode in ReplySuccessContent optional (fe836f4)
  • nano-server: make new package (2148dfc)