Node+Express.js RESTApi 上的业务逻辑放在哪里

Dou*_*ais 6 javascript model-view-controller node.js express

我有以下 Node+Express.js RESTApi 架构:

- controllers
- db
-- models
-- config
-- migrations
- helpers
- routes
- services
- test
package.json
index.js
Run Code Online (Sandbox Code Playgroud)

放置业务规则的正确位置在哪里?(控制器、模型、服务?)

EQu*_*per 6

从你的文件夹结构来看,这就是我将如何制作它。PS 这就是通过读取文件夹名称来思考您的项目结构的方式,这并不完美,但也许可以帮助您。

- controllers -> logic before saving to the db, check permission, etc..
- db
-- models -> all the stuff who touch a models himself, the schema,etc
-- config -> config about the db, connection to the db etc
-- migrations -> all the migrations file for the db
- helpers -> helpers function like sum, total, pluralize, etc
- routes -> all the rest api route, where they take a controller as callback
- services -> stripe, aws s3 etc
- test -> all your test
package.json -> all your dependencies
index.js -> where everything start, your server instance etc
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,业务逻辑位于控制器中。模型可用于添加涉及 db 值等的逻辑。