TEN Stack: Typescript-Express-Node template (Updated 2022)
Tags: typescript, express, node
Published: 2022-08-07 18:16:00

## Overview

**TEN stack** is a `Typescript` + `Express` + `Node` starter kit to develop `REST API` server app.
Nothing new under the sun, just a straight forward combo to make server development a little bit faster. This template is perfect for anyone who is starting on the server-side world based on Node.js

This template comes with:

- Class-based OOP architecture with DI & DAO (scalable)
- Equipped for TDD with [Jest](https://jestjs.io/docs/getting-started)
- Everything strictly typed with [Typescript](https://www.typescriptlang.org/)
- [ES6](http://babeljs.io/learn-es2015/) features/modules
- ES7 [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) / [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
- Global error handling architecture with templates (ApiError, TimeOutError, ValidationError, etc)
- Request validation as custom middleware with [Express-validator](https://express-validator.github.io/docs/)
- Blazing fast logging with [Express-Pinno-Logger](https://github.com/pinojs/express-pino-logger)
- Run with [Nodemon](https://nodemon.io/) for automatic reload & watch
- [ESLint](http://eslint.org/) for code linting
- Code formatting using [Prettier](https://www.npmjs.com/package/prettier)
- Configuration management using [dotenv](https://www.npmjs.com/package/dotenv)
- Improved commits with [Husky](https://typicode.github.io/husky)(v8) - pre-commit & pre-push
- Manage production app process with [PM2](https://pm2.keymetrics.io/)
- CI/CD workflows with [Github Actions](https://github.com/features/actions)
- Editable Github Pull requests & Issue templates

  <br>
  <br>

---

## Prerequisites

- [Node.js](https://nodejs.org) (`>= 16.x`)
- [Yarn](https://yarnpkg.com/en/docs/install) or [NPM](https://docs.npmjs.com/getting-started/installing-node)

## Install

- Fork or Use [this](https://github.com/filoscoder/tenstack-starter/generate) template repository.
- [Clone](https://github.com/git-guides/git-clone) the forked repository.
- Install the dependencies with [yarn](https://yarnpkg.com/getting-started/usage) or [npm](https://docs.npmjs.com/cli/v7/commands/npm-install).

> Make sure you already have [`node.js`](https://github.com/filoscoder/tenstack-starter#prerequisites) and [`npm`](https://github.com/filoscoder/tenstack-starter#prerequisites) or [`yarn`](https://github.com/filoscoder/tenstack-starter#prerequisites) installed in your system.

- Set your `git remote add origin` path

```bash
 git remote add origin ${forked-and-cloned-path}
```

> [Update the url](https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#changing-a-remote-repositorys-url) if you already have an `origin`

<br>
<br>

## Config

- Copy `.env.example` a file at the root of the application.
- Add or modify specific variables and update it according to your need.

```bash
 cp .env.example .env
```

> Check the `config` folder to customize your settings (`/src/config`)

<br>
<br>

## Alias @

To make paths clean and ease to access `@` is setup up for `/src` path

```javascript
// BEFORE
import config from './config';
import routes from './routes';

// NOW
import config from '@/config';
import routes from '@/routes';
```

> You can customize this setup:
> `/tsconfig.json` > compilerOptions.baseUrl & compilerOptions.paths

<br>
<br>

## Local Development

Run the server locally. It will be run with Nodemon and ready to serve on port `8080` (unless you specify it on your `.env`)

```bash
 yarn start # or npm start
```

> Check [`package.json`](https://github.com/filoscoder/tenstack-starter/blob/master/package.json) to see more "scripts"

<br>
<br>

## Continuous Integration & Continuous Deploy (CI/CD)

The workflows are under the `.github` folder
CI will be triggered on every PR update
CD will be triggered when specified branches got pushed (ex. `main` , `develop`)

If need more info about the setting, check [this](https://medium.com/atant/ci-with-github-actions-feat-slack-notification-dd7e89a1d80a) article

## Production

First, build the application.

```bash
 yarn build # or npm run build
```

Then, use [`pm2`](https://github.com/Unitech/pm2) to start the application as a service.

```bash
 yarn service:start # or npm run service:start
```

<br>
<br>

## Contribution

Check the source [here](https://github.com/filoscoder/tenstack-starter)
Please feel free to open an `issue` or a `pull request` to suggest changes or additions.