Skip to content

aquariophilie/node-koa-boilerplate

Repository files navigation

node-koa-boilerplate

GitHub Super-Linter

A boilerplate for a simple API in Node.js using the Koa.js framework with TypeScript and dependency injection using InversifyJS.

Prerequisites

  • Node.js v14.x or later
  • Connection details (URI, user, password and db name) to a running instance of MongoDB

Prerequisites for Kubernetes deployment with Helm chart

  • You have kubectl installed and configured for your cluster

  • You have a Kubernetes cluster like Minikube

  • You have the Helm command-line installed

  • You have created and published a Docker image for your application, in this example we use the public repository rafaelesposito/node-koa-boilerplate

Installation

Use the package manager npm to install the dependencies.

npm install

For the authentication in this example we are using a JWT token with "ES256" algorithm and async keys. For generating private and public keys for authentication use OpenSSL. We are also using a short-lived token and a long-lived refresh token.

openssl ecparam -genkey -name prime256v1 -noout -out private.pem
openssl ec -in private.pem -pubout -out public.pem

Usage

Runtime customization

Before running the code you need to customize its runtime enviroment. Create an .env file starting from the provided .env.example, then customize it as described in the comments (for instance you need the supply the database configuration).

Initialize (or migrate) database

# View database migrations status
npm run migrate:status
# Run database migrations
npm run migrate:up

Start in development mode

# Start in development mode with nodemon
npm start

Build for production

Make sure your .env file has the line NODE_ENV=development commented out, then run

# Build
npm run build

Deploy to Kubernetes using the chart

From the directory chart run:

# Deploy with helm
helm install my-release koa-mongo-k8s

For changing the database username and password create the secrets with base64 encoded:

echo -n 'username' | base64
dXNlcm5hbWU=

echo -n 'password' | base64
cGFzc3dvcmQ=

and replace the values in the secret.yaml file.

API documentation

Documentation built with Swagger is available under "api/swagger-html" (only in development environment).

Copyright and license

Copyright (C) 2022 by the Aquariophilie team, all rights reserved.

The code contained in this repository and the executable distributions are licensed under the terms of the MIT license as detailed in the LICENSE file.