This is the template for Node project which has ExpressJS as backend framework.
What this project has been setup:
- babel (source code transformations)
- jest (testing framework)
- supertest (for http server's intergration tests)
- eslint (lint)
- prettier (code formating)
- husky (for pre-commit hook)
Requirements:
- Node (version 12)
- Yarn
- Docker (for building docker image)
Note: You can use nvm to switch between node versions easily.
Clone project:
git clone git@github.com:PercyPham/express-app-template.git [your-app-name]
Recommend using VSCode for developing this project.
After installing VSCode, add extra extensions to VSCode:
Config VSCode's workspace settings as below:
{
"settings": {
"window.zoomLevel": 1,
"editor.formatOnSave": true,
"eslint.autoFixOnSave": true
}
}
Install dependencies before starting development:
yarn install
Start project in development mode:
yarn dev
This project already has lint set up.
To check lint for your code, run:
yarn lint
Check and fix your code before commit:
yarn lint:fix
Note: Test code will get env variables defined in
.env.test
file.
Unit test:
# normal run
yarn test:unit
# run in watch mode
yarn test:unit --watch
Integration test
yarn test:integration
All test with coverage report (visual result will be placed in .coverage
folder at same level with this README file)
yarn test:coverage
All test file should be placed under __tests__
folder next to its tested function file.
Test files can also be placed inside test
folder.
Unit test file name must be end with .unit.test.js
Integration test file name must be end with .int.test.js
For example:
utilFunc
├── __tests__
│ └── utilFunc.unit.test.js
├── utilFunc.js
└── index.js
Before building make a copy of .env.dev
and name it .env
And then change its content accordingly.
Run:
yarn build
Note: this will output the built folder dist
.
To run the built code, simply run:
yarn start
Run:
docker build -t [your-app-name] .
Run container:
docker run -p 5000:5000 [your-app-name]
Note: suppose your app use default port 5000 (not defined it in .env
file).