Structuring Node.js Project
Truong Vu
Posted on November 22, 2018
If you are looking for the NodeJS project structure based on the best practices to build the RESTful API. It might be helpful for you guys.
Some of the good practices followed in this repository:
- Code Style Practices via Node Best Practices
- ES6 Support.
- Morgan Logger.
- Error Handling.
- Open API Specification implemented through apidocjs.
- JWT Authentication.
- Joi & Express Validation.
- Environment variables via
.env
file. - Linting with Prettier.
- Security (Helmet, CORS, Express Brute).
- Husky as git hook for linting & running unit test before commit.
- Unit & E2E Testing with Jest.
- Backpack build system
Prerequisite
- NodeJS
- Node Package Management (
yarn
ornpm
) - Docker
Installation
- Clone the project
git clone git@github.com:vukhanhtruong/nodejs-api-boilerplate.git
. - Install dependencies
yarn install
ornpm i
- Copy
.env.example
to.env
file.
Running Docker
docker-compose up -d
.
Raven Log
Create account at Sentry, then put your url to .env
file at variable SENTRY_DSN
.
Api Doc
Api Doc is hosted on surge. For change the url and have your own docs just modify your link in the .env
file. Running the following command to publish your documentation.
# Generate document
yarn doc
# Deploy document to surge.sh
yarn doc:deploy
# or
npm run doc
npm run doc:deploy
Pre-Commit Hook
Using husky
for linting your code before commit & running unit test before push.
Scripts
DEV
yarn dev
# or
npm run dev
DEBUG
Debug with VSCode. See VSCode Auto-Attach
yarn debug
# or
npm run debug
TEST
yarn test
# or
npm run test
NOTE: If you have the issue with ENOSPC, run the below command to avoid:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
Test Watch
yarn test:watch
COVERAGE
# run jest coverage
yarn test:cover
# show html report
yarn test:cover:open
# or
npm run test:cover
npm run test:cover:open
JSDOC
# generate documentation
yarn doc
# Publish documentation to surge.sh
yarn doc:deploy
#or
npm run doc
npm run doc:deploy
Find out more here
Happy Coding!
Posted on November 22, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.