我有以下的Node+Express.js RESTApi架构:
- controllers
- db
-- models
-- config
-- migrations
- helpers
- routes
- services
- test
package.json
index.js
什么地方是放置业务规则的正确位置?(控制器、模型还是服务?)
我有以下的Node+Express.js RESTApi架构:
- controllers
- db
-- models
-- config
-- migrations
- helpers
- routes
- services
- test
package.json
index.js
从您的文件夹结构中,我了解到您的项目可能以以下方式进行组织。顺便说一下,这仅基于文件夹名称而得出的推测,并不是完美的方案,但或许能为您提供帮助。
- 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
在您的情况下,业务逻辑应该放在控制器中。模型可以用于添加与数据库值相关的逻辑等。