π 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.
> cp .env.example .env
> vi .env
> git clone https://github.com/PW486/express-ts-starter.git
> npm install
> npm run watch
> npm test
> echo "NODE_ENV=production" > .env
> npm run build
> pm2 start ecosystem.config.js
- 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
Category | Name |
---|---|
Language | TypeScript |
JS Runtime | Node |
Web Framework | Express |
Database | PostgreSQL |
ORM | TypeORM |
Test Framework | Jest |
Authentication | JWT |
Linter | TSLint |
Formatter | Prettier |
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.
- 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
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
.
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.
Copyright Β© 2019 DONGGEON LIM.
This project is MIT licensed.