Skip to content

peterkitonga/nodetsstarter

Repository files navigation

Node TS Starter

Build Status Coverage Status

This starter was created to serve as a starting template for a Node.js API built with Express and TypeScript.

Features

  • TypeScript - For type safety and other awesome features not native to vanilla JavaScript.
  • Express - Micro-framework for setting up routes, middlewares, controllers.
  • Nodemailer - For all your mailing needs.
  • Mongoose - ODM for connection to and querying of MongoDB collections.
  • AWS S3 SDK - For all your file storage needs. Requires an AWS account and S3 bucket.

Roadmap

  • Database connection with mongoose
  • Linting with eslint and prettier
  • Mailing service with nodemailer
  • Error handling middleware
  • Logging setup for errors and data
  • Handle graceful shutdown of server
  • API validation logic with joi
  • Unit tests with mocha, chai & sinon
  • Continuous integration with TravisCI
  • Coverage reports with istanbul & Coveralls
  • Switch to JEST for unit tests
  • Dependency injection for the service layer
  • Containerized setup with docker-compose

Folder structure

|-- data/                     # Database files for the mongo docker container
|-- public/                   # Static assets & symlinks for locally stored files
|-- src/
|   |-- @types/               # Configurations for declaration merging
|   |-- api/
|   |   |-- controllers/      # All REST API router functions
|   |   |-- middlewares/      # Interceptor filters before router functions
|   |   |-- routes/           # Routes for the REST API
|   |-- configs/              # Configurations retrieved from env variables
|   |-- core/                 # Core modules split for easier startup of the application
|   |-- models/               # Mongoose models for retrieving mongodb documents
|   |-- repositories/         # Database query classes as data access layer
|   |-- services/             # Main business logic code as service layer
|   |-- shared/               
|   |   |-- abstracts/        # Custom abstract classes
|   |   |-- decorators/       # Custom decorators
|   |   |-- enums/            # Custom enum types
|   |   |-- errors/           # Custom error classes with HTTP status codes
|   |   |-- interfaces/       # Custom interface types
|   |-- utils/                # Local utility functions for application
|   |-- app.ts                # Application main file
|-- storage/                  # Local storage for cache, files and logs
|-- tests/                    # Specs for unit tests
|-- tools/                    
|   |-- docker/               # Dockerfiles for custom images
|   |-- nginx/                # Configuration files for the nginx reverse proxy
|-- views/                    # Templates for emails & pdf files(e.g. invoices)
|-- .env.example              # Local environment config sample

Environment Variables

To begin using the starter project, first copy the example variables into a .env file with command: cp .env.example .env. You will then need to modify the following environment variables in the .env file for your API to run:

  • APP_PORT, APP_BASE_URL, APP_LOG_LEVEL, APP_LOCALE, APP_TIMEZONE

  • NODE_ENV - production or development

  • MONGO_PROVIDER - local or atlas(for MongoDB Atlas)

  • MONGO_HOST - if atlas, pass the domain provided after the @ symbol when you click on connect > Connect your application

  • FILE_SYSTEM_PROVIDER - local or s3(AWS S3)

  • FILE_SYSTEM_LIMIT - maximum file size to be allowed. Note: this affects the maximum request body size of Express

  • CLIENT_BASE_URL - base url for the front-end client application

Other configurable variables will all be listed in the created .env file.

Run Locally

Clone the project

  git clone https://github.com/peterkitonga/nodetsstarter

Go to the project directory

  cd nodetsstarter

Install dependencies

  npm install

Start the development server

  npm run serve

To run the development server with docker, run

  docker-compose up -d dev-server

Deployment

Install dependencies first

  npm install

To deploy this project in production, run

  npm run build

To deploy this project in production with docker, run

  docker-compose up -d deploy-server

Running Tests

Tests are written in Mocha, Chai under the test directory and uses istanbul for coverage reports. To run tests, run the following command

  npm test

To run and view coverage reports for the tests, you will need to run two scripts in sequential order

  npm run test:coverage ; npm run test:view

Tests can also be run with docker using the npm docker "utility" service provided. Run this with

  docker-compose run --rm npm test

Linting

Linting is configured with eslint and prettier. You are free to configure it to your liking using the configuration files .eslintrc and .prettierrc

To check for linting errors, run

  npm run lint

To format code with linting rules, run

  npm run lint:format

Authors

Peter Kitonga