Skip to content

PW486/express-ts-starter

Repository files navigation

TypeScript Express Starter Kit
Travis (.com) GitHub issues GitHub forks GitHub stars GitHub license GitHub code size in bytes GitHub last commit David

πŸš€ Quick Start TypeScript Express

This is the initial structure of a project. If you are trying to start a backend project with express, this kit is possible to minimize troublesome work.

Getting Started

Set Environments

> cp .env.example .env
> vi .env

Clone & Install Dependencies

> git clone https://github.com/PW486/express-ts-starter.git
> npm install
> npm run watch

Testing

> npm test

Prepare Deploying

> echo "NODE_ENV=production" > .env
> npm run build
> pm2 start ecosystem.config.js

Developing

  • Remove local branches deleted on the remote server
    > git fetch -p && for branch in `git branch -vv --no-color | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
  • Keep the linter and formatter rules
  • Check unused, outdated states of dependencies: depcheck npm-check-updates

Tech Stack

Category Name
Language TypeScript
JS Runtime Node
Web Framework Express
Database PostgreSQL
ORM TypeORM
Test Framework Jest
Authentication JWT
Linter TSLint
Formatter Prettier

Routing Example

POST /api/v1/posts

{
  path: '/posts',
  method: 'post',
  auth: true,
  permission: ['admin'],
  upload: imageUpload.single('photo'),
  validator: postPostValidator,
  handler: postPostHandler,
}

Manage all options in one object. auth, permission, upload, validator, and handler are processed in the order.

Generating Migration

  • Auto generate : npm run typeorm migration:generate -- -n <migration-name>
  • Create empty file : npm run typeorm migration:create -- -n <migration-name>
  • Run migration : npm run typeorm migration:run
  • Revert migration : npm run typeorm migration:revert

Project Structure

API Directory

api
β”œβ”€β”€ post
|  β”œβ”€β”€ post.entity.ts
|  └── v1
|     β”œβ”€β”€ handler
|     |  β”œβ”€β”€ post.delById.ts
|     |  β”œβ”€β”€ post.getAll.ts
|     |  β”œβ”€β”€ post.getById.ts
|     |  β”œβ”€β”€ post.post.ts
|     |  └── post.putById.ts
|     β”œβ”€β”€ index.ts
|     β”œβ”€β”€ post.route.ts
|     β”œβ”€β”€ post.test.ts
|     └── post.validator.ts
└── account
   β”œβ”€β”€ account.entity.ts
   β”œβ”€β”€ v1
   |  β”œβ”€β”€ action
   |  |  └── account.getTokenById.ts
   |  β”œβ”€β”€ handler
   |  |  β”œβ”€β”€ account.getToken.ts
   |  |  β”œβ”€β”€ account.postSignIn.ts
   |  |  └── account.postSignUp.ts
   |  β”œβ”€β”€ index.ts
   |  β”œβ”€β”€ account.route.ts
   |  β”œβ”€β”€ account.test.ts
   |  └── account.validator.ts
   └── v2

There are collection directories within API. Each collection contains <collection-name>.entity.ts and different files(route validator handler action test) for each version. Action is a function that makes code duplicated in a handler.
If you create a route in the v1, v2 directories, the endpoint is automatically prefixed with v1, v2.

Other Directories

src
β”œβ”€β”€ app.ts
β”œβ”€β”€ server.ts
β”œβ”€β”€ config
|  β”œβ”€β”€ environments.ts
|  β”œβ”€β”€ errorHandlers.ts
|  β”œβ”€β”€ middlewares.ts
|  └── routes.ts
β”œβ”€β”€ migrations
|  └── <timestamp>-<migraion-name>.ts
β”œβ”€β”€ types
|  β”œβ”€β”€ error.d.ts
|  β”œβ”€β”€ route.d.ts
|  └── user.d.ts
└── utils
   β”œβ”€β”€ entity.ts
   β”œβ”€β”€ error.ts
   β”œβ”€β”€ logger.ts
   └── upload.ts

Other directories contain app configuration, db migration, typescript declaration and utility files. Config is a directory of files to set up before listening to the express app, but utils directory contains utilities used in various places. And types directory contains the declares used by most collections.

Demo

Create React Ant Design Boilerplate

SwiftUI Skeleton App

License

Copyright Β© 2019 DONGGEON LIM.
This project is MIT licensed.