A template that can be used to quickly scaffold a project based on express, knex and typescript.
Buzzwords ...or... What technologies does this project use:
- Docker: Linux container runtime
- PostgreSQL: SQL Database
- Knexjs: Query Builder library
- Mocha & Chai: Testing libraries
- Nodemon: Node process watcher/restarter for fast development
- TypeScript: JavaScript + Types = <3
- Pug: HTML/XML/Plain Text Template engine
- ESLint: linting library that enforces code style
- Express: HTTP server library that provides a middleware-based architecture
- BullMQ: Job Queue management library
- Redis: In-Memory store used by BullMQ
- JWT: Json Web Tokens used for authorization/authentication purposes
- dotenv: library that imports environment variables from a
.env
file - Morgan & Winston: Logging middleware and library
- bcrypt: cryptographic library used for hashing
If you're comfortable testing it, you own it, start by reading tests in the project.
Clone the project:
git clone https://github.com/shalior/express-ts-template.git
cd express-ts-template
npm i
npm run setup
This app reads its environment variables from a .env file, a .env.example is provided.
The npm run setup
command will copy the content of the .env.example and replace the SECRET variable with a cryptographically-safe random string.
Moreover, the setup script will also rename the package name in both package.json and package-lock.json
Note: never add the .env file to git or any other version control system. It's meant to be a local file with custom configurations relative to the machine where the app runs
This template provides a set of useful scripts that can be called using the npm run <script>
syntax.
These scripts are:
Normally you'll run these with my docker helper script (nail) like nail npm run build
knex
: knex cli wrapper that runs dotenv/config before instantiating knextest
: tests the application using mocha and chaibuild
: runs the typescript compiler to build your applicationprestart
: runs all the pending migration before thestart
script using themigrate-nobuild
scriptstart
: starts a node process that will execute this packagedev
: starts nodemon in watch mode, this way you can edit your source ts files without having to rebuild and restart the application manuallylint
: runs eslintlint:fix
: runs eslint with the --fix flagmigrate
: builds the project and runs all the migrations under src/db/migrationsmigrate-nobuild
: runs all the migrations under src/db/migrationsrollback:all
: builds the project and rollbacks all the migrations under src/db/migrationsrefresh:seed
: rollback all migration and run seeders.seed
: builds the project and runs all the seeds under src/db/seedsmigrate-seed
: migrates and seeds, without building twicesetup
: runs the setup.js script described below
The docker-compose.yml file is intented to be used in development, It has images for node postgres and redis. to interact with containers use the nail helper like so:
docker compose up -d
: spin up docker composenail npm run build
: runs build script inside container using node imagenail test:all
: runs all testsnail mocha --grep="keyword"
to run specific set of tests
nail has other useful commands like: nail psql
, nail shell
and others, for a complete list look at the source code.
If you look for the word TODO
in this project you'll find some places where your intervention could be needed to better fit the needs of your new project. Feel free to
modify anything you want!
This project is a maintained and enhanced fork of express-knex-typescript-template