Nodejs project directory structure design

design

To achieve a certain project structure, it is not enough to divide the project into several folders. The ultimate purpose of the project structure design is to reduce the complexity of the system. In general design, a system is always divided into several small systems. Each small system completes a function by itself, and to control the access rights between various subsystems, it is easy to implement package permissions in java. But nodejs has only modules and no package permissions. Therefore, at the beginning of the project, the package permissions must be realized through conventions. Conventions are the rules that everyone consciously abides by when writing code. Consciousness is very important. Observe the access rights between the various subsystems and do not exceed them.

Because of the diversity of data, in order to ensure the consistency of the external performance of the data layer, because the data model of each project is different, the selected database is also different. In order to ensure the external consistency of the model layer, and to protect the data, in the A proxy layer is added on top of the model layer. All data access needs to go through the proxy layer.

In mvc, the model layer corresponds to the resource model of the website. The model cannot be simply understood as a table in the database. It is abstract. It can be a picture, a file, a database table, or several tables. and together.

Directory Structure

  • bin
  • controllers
  • logs
  • middlewares
  • models
  • proxy
  • public
  • services
  • test
  • views
  • app.js
  • config.js
  • router.js
  • package.js

express request response process

process

Process analysis

The arrows in the above figure represent the flow of requests or access permissions.
router: only do request distribution, no business logic
err is a unified error handling location, record errors, and unify err pages.
middlewares: business middleware, such as user rights control
model can only be accessed by its proxy, and proxy can be accessed by controller and service.
The main logic of the controller handles
the service as the business component of the project. Such as: redis connection service, cache component, log component, etc., he can reduce the burden on the controller.
app: put one-time scripts

Project code github

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726086&siteId=291194637