Base source for Node projects
-
Respond with error
- Using
throw
withVError
to response with error
- Using
-
Logger
- Using VLogger to log in source code
- Log rules:
logger.info
: when you need to store an information of an actionlogger.error
: when you need to log an errorlogger.warn
: when you need to log warning, it doesn't affect current flow but can affect system in the futurelogger.debug
: when you need to log something make you debug easier
- Change log level according to deploy environment
-
Coding convention
- Reference: https://github.com/airbnb/javascript
- All third-party lib must be imported on top of file
-
Dependencies
- Must not import sibling file directly. For example:
- Service cannot import another service directly, must add it as a dependencies and pass to
service factory
instead.
- Service cannot import another service directly, must add it as a dependencies and pass to
- Must not import code of upper layer. For example:
- Service can import common functions, helpers functions, but not vice versa.
- Must not import sibling file directly. For example:
-
Define JSDoc
- Use
/**
to quickly create jsdoc - Reference: https://code.visualstudio.com/docs/languages/javascript#_jsdoc-support
- Use
-
Recommend VSCode extensions
- GitLens
- Code Spell Checker
- Javascript(ES6) Code Snippets
- Todo Tree
-
Recommend resources
- Clean code architecture: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
-
Examples
- Use redis command
const { getRedisClient } = require('../../components/redis'); ... const redisClient = getRedisClient(); redisClient.set('hello', 'world'); ...
-
Create new module directory in
/modules
-
Define new route
- Create new api route in
api.js
- Attach middlewares:
verify token
,validate request data
, ... - Import controller
- Create new api route in
-
Define middleware, validation
- Create new validation to validate input data of this api
- Using
Joi
schema to validate data, you can find guide here - Using
validate
built-in function ofexpress-validation
to attach to API like a middleware
-
Define new controller
- Import services and dependencies
- Create service with
serviceFactory
- Extract data input from request
- Respond using
res.success
- Wrapper method of controller with
tryCatchWrapper
inconstructor
-
Define new service
- Write business logic in method of class Service
- If business need to combine many services, create a
business
file
-
Define new business (optional)
- Create a business file, import all necessary services
- Write
business logic
in method of class Business - Import business file inside controller file
-
Define new model
- To mapping Data with DB, you should create a model according to a collection in MongoDB
- Define options with
collection
, schema of model in/modules/.../model.js
-
Define new repository
- To interact with DB, you should create a repository
- A new repository will extends from a
BaseRepository
- If you want to create some custom action to DB, you can create method in repository file.
-
Add checker in every file
- Add
// @ts-check
on top of file, see more here
- Add
- Mustn't import file of the same level in the other. For example:
- Service mustn't import other services.
- Repository mustn't import other repositories.
- Installation Prometheus: https://github.com/VNLP-Tech/node-metrics
- Run
npm install
to install packages - Run
npm start
to start server - Run
npm run test
to run tests - Run
npm run dev
to start server with DEBUG level - Run
npm run lint
to find coding convention issues
- Add pre-commit, lint to clean code
- Add redis connection
- Create metrics with Prometheus
- Add log to monitor slow query DB
- Create websocket components
- Create Dockerfile
- Create documentation with Swagger
- Create cli command to generate module in one command
- Add unit test, integration test
- Add k6 test script for load test
- Add swagger to generate docs
- Everyone can create PR to this repo to contribute to this source
- If you have a problem with this source, you can add an issue and tag
@duysmile
or email meduy210697@gmail.com