Nest is a progressive Node.js framework for building efficient and scalable server-side applications. This project was generated with Nest CLI version 7 and Node.js version 14. It has a complete development environment configured, including build, test, and deploy scripts as examples.
- Project structure
- Available npm scripts
- Code scaffolding
- Development mode
- Linting and formatting code
- Running unit tests
- Running end-to-end tests
- Debugging
- Security, performance and best practices
- Commit messages convention
- Build and deployment
- Support
When working in a large team with many developers that are responsible for the same codebase, having a common understanding of how the application should be structured is vital. Based on best practices from the community, Nest and Angular coding style guide, other github projects and developer experience, your project should look like this:
├── ci
├── e2e
| ├── page-objects
| ├── specs
| | └── app.spec.ts
| └── jest.config.js
├── src
| ├── app
| | ├── app.controller.spec.ts
| | ├── app.controller.ts
| | ├── app.module.ts
| | ├── app.service.spec.ts
| | └── app.service.ts
| ├── assets
| ├── environments
| | ├── environment.prod.ts
| | └── environment.ts
| ├── main.ts
| └── swagger.ts
├── .commitlintrc.json
├── .editorconfig
├── .eslintrc.json
├── .gitignore
├── .lintstagedrc.json
├── .prettierrc.json
├── CHANGELOG.md
├── jest.config.js
├── nest-cli.json
├── package-lock.json
├── package.json
├── README.md
├── tsconfig.build.json
├── tsconfig.json
└── webpack.config.js
All of the app's code goes in a folder named src
. The end-to-end tests are in the e2e
folder.
In Nest, everything is organized in modules, and every application have at least one of them, the app
root module.
The app
module is the entry point of the application and is the module that Nest uses to bootstrap the application.
Static files are placed in assets
folder.
The scripts in package.json file were built with simplicity in mind to automate as much repetitive tasks as possible and help developers focus on what really matters. There are commands to start the application, code linting and formatting, to run unit tests, to run e2e tests, to generate project documentation via Compodoc and project changelog, npm audit, to build, release and deploy the application to Docker Swarm, and others. All the commands should be executed in a console inside the root directory.
Run nest generate controller controller-name
to generate a new controller.
You can also use nest generate filter|pipe|service|class|guard|middleware|gateway|module|decorator|interceptor
.
Use npm run start
to run the app in the development mode.
This app includes Swagger. It is available at http://localhost:3000/api.
The OpenAPI Specification is exportable by running npm run swagger
script.
Use npm run lint
to analyze your code. It includes, ESLint
and Prettier
.
Many problems can be automatically fixed with npm run lint:fix
.
Depending on your editor, you may want to add an editor extension to lint and format your code while we type or on-save. To ensure all files committed to git don't have any TypeScript, linting, or formatting errors, there is a tool called lint-staged that can be used. When lint-staged is used in combination with husky, the linting commands specified with lint-staged can be executed to staged files on pre-commit.
Use npm run test
to execute the unit tests via Jest.
Use npm run test:watch
to keep executing unit tests in real time while watching for file changes in the background.
You can see the HTML coverage report opening the index.html file in your web browser.
If you want to exclude a specific test, simply use xit()
or xdescribe()
.
If you want to run a specific test, use fit()
or fdescribe()
.
The x
means exclude and the f
stands for focused.
Use npm run e2e
to execute the end-to-end tests via Jest and SuperTest.
Use npm run e2e:watch
to keep executing your tests while watching for file changes in the background.
You can see the HTML report opening the index.html file in your web browser.
You can debug the code, add breakpoints, inspect variables and view the application's call stack.
Also, you can use the IDE for debugging unit and end-to-end tests.
These functionalities are provided natively or based on plugins.
You can debug tests in chrome inspector with debugger
keyword if you run npm run test:debug
or npm run e2e:debug
.
When you are using the debug scripts, you need to open the chrome://inspect
page.
The npm audit
command submits a description of the dependencies configured in your package to your default registry and asks for a report of known vulnerabilities
You can also have npm automatically fix the vulnerabilities by running npm audit fix
.
By default, Nest makes use of the Express framework. Fastify provides a good alternative framework for Nest because it solves design issues in a similar manner to Express. However, Fastify is much faster than Express, achieving almost two times better benchmarks results.
Code conventions are base rules that allow the creation of a uniform code base across an organization. Airbnb JavaScript Style Guide is very popular and recommended. You can complete them with rules just for TypeScript.
In order to have a consistent git history every commit must follow a specific template. Here's the template:
<type>(<ITEM ID>?): <subject>
Must be one of the following:
- build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, Jenkins, SauceLabs)
- chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
- docs: Documentation only changes
- feat: A new feature
- fix: A bug fix
- perf: A code change that improves performance
- refactor: A code change that neither fixes a bug nor adds a feature
- revert: A commit that reverts a previous one
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- test: Adding missing tests or correcting existing tests
The related issue or user story or even defect.
- For user stories, you shoud use
US-
as prefix. Example:feat(US-4321): ...
- For no related issues or defects you should leave it blank. Example:
feat: ...
The subject contains a succinct description of the change.
Run npm run build
to build the project. The build artifacts will be stored in the dist
directory.
In ci
folder you can find scripts for your Jenkins CI pipeline, a Dockerfile and an example for deploying your application with Ansible to Docker Swarm.
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.