1.0.0 • Published 4 years ago
structure-app-nodejs v1.0.0
VNLP source base
Base source for VNLP projects
Guide
Respond with error
- Using
throwwithVErrorto 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 factoryinstead.
- 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
Step by step to create a new module
Create new module directory in
/modulesDefine 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
Joischema to validate data, you can find guide here - Using
validatebuilt-in function ofexpress-validationto 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
tryCatchWrapperinconstructor
Define new service
- Create a service inside directory
/modules/.../services - In service file, create a class
Servicewith input are list ofdependenciesand return a function - Write business logic in service function
- Create a service inside directory
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.
Integration
- Logger (Pino with ELK stack)
- Tracing (Jaeger)
- Message Queue (bull, RabbitMQ or Kafka)
Run
- Run
npm installto install packages - Run
npm startto start server - Run
npm run testto run tests - Run
npm run devto start server with DEBUG level
TODO
- Add pre-commit, lint to clean code
- Add redis connection
- Add log to monitor slow query DB
- Create websocket components
- Create Dockerfile
- Create documentation with Swagger
- Create metrics with Prometheus
- Create cli command to generate module in one command
Contribute
- VNLP team members 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
@duysmileor email meduy210697@gmail.com
1.0.0
4 years ago