Skip to content

ridakk/nodejs-express-typescript

Repository files navigation

minimal NodeJs Express starter with TypeScript

Build Status Coverage Status David Snyk Vulnerabilities for GitHub Repo Commitizen friendly

Usage (quick start)

  1. clone the repo
  2. npm install
  3. Setup temp environment configs (TEST only)
    • RUN in CLI from project root ./setenv.sh if you have bash compatible cli
    • OR create .env file manually
  4. Make note of generated files and change to your preferences
    • IMPORTANT: when deploying app, don't use the .env file, simply set vars in your CI provider or container manager
  5. Run eslint
    • npm run lint
  6. Run tests (will load up test data in tables)
    • npm run test
  7. Run coverage (will load up test data in tables)
    • npm run coverage
    • see results from coverage/lcov-report/index.html
  8. Start up app in developer mode (will watch and recompile for changes)
    • npm run dev
  9. Start up app in production mode (will watch and recompile for changes)
    • npm run build ? npm start
  10. Open browser tab to Swagger UI Explorer to explore API

Features

Api Documentation

Used swagger-jsdoc for generating Swagger using JSDoc comments in code.

See health route for a simple usage example.

Configuration Management

Deployment specific hierarchical configurations are maintained with config module.

To add a system wide configuration parameter, update ./config/default.yaml. To add/update an environment or deployment specific configuration parameter, update for ex: ./production.yaml

IMPORTANT: do NOT put critical parameters like keys/passwords in configuration files, and use .env file described below.

Environment Variables

RUN in CLI from project root ./setenv.sh if you have bash compatible cli, if not create an .env file accordingly to the script. Make note of generated files and change to your preferences IMPORTANT: when deploying app, don't use the .env file, simply set vars in your CI provider or container manager

adding a new environment variable: 1- update ./setenv.sh and add a default value 2- update ./env/validators.ts to validate the value

import { str } from 'envalid';

export default {
  // put env validations here
  // Envalid parses NODE_ENV automatically, so no need to put it here
  DUMMY: str(),
};

And you can use the env variable as below;

import env from  './env';

console.log(env.DUMMY);

Logging

Pino is used as a logging library in combination with express pino logger and pino pretty

See logger for a basic pino configuration.

Testing

This demo app includes automated tests using supertest and jest to test routes, etc.

See example for a simple test route.

Commit guideline

This repo is Commitizen-friendly and using use AngularJS's commit message convention also known as conventional-changelog.

To use Commitizen command line interface:

npm run cm

For validating commit message format for users using git commit, commitlint is used to check if the commit messages meet the conventional commit format.