我具有以下Node + Express.js RESTApi体系结构:
- controllers
- db
-- models
-- config
-- migrations
- helpers
- routes
- services
- test
package.json
index.js
在何处放置业务规则? (控制器,模型,服务?)
答案 0 :(得分:1)
ExpressJS有点新,可喜的是它不像Rails和有关组织代码的常规PHP框架那样规范。
我正在以这种方式构造代码:
答案 1 :(得分:0)
从您的文件夹结构来看,这就是我的制作方法。 P.S这就是我仅通过读取文件夹名称就对您的项目结构的看法,这虽然不完美,但可能会对您有所帮助。
- 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
因此,您需要将业务逻辑放入控制器中。模型可以用来添加触及db值等的逻辑。