When I started developing in Node.js, in my projects I used structure like this - controllers - user.js - someFeature.js - someAnotherFeature.js - routes - user.js - someFeature.js - someAnotherFeature.js The controllers - are express middlewares that contain all business logic Routes - are express routes that import controllers/middlewares and use itBut that brought a lot of complexity so I started splitting files by what they are/do: - user - user.ctrl.js - user.router.js - user.helpers.js That made things a bit easier. But after some time I needed to reuse some logic from one controller in another and I realized that keep all business logic in express controller (middleware) wasn't the best idea. And I ended up creating *.service.js files that contain business logic so I'll be able to reuse it somewhere in the future (and testing would become easier as well). So the whole project structure would look something like this: ``` - api // the API containg all business logic - user - user.ctrl.ts // express middleware for router. Delegates all business logic to service - user.router.ts - user.middlewares.ts - user.service.ts // Makes all real work - user.queries.ts // queries to the db - user.helpers.ts // formatting/sorting functionsservices // Some 3d party servicesgoogleService.tsutilsadditionalHelper.tsdbmysql.tsapp.ts // Application entry point ``` But I'm pretty sure it could be better. So how do you structure your apps and where do you keep your business logic or any advices how to make it better?
Submitted April 20, 2019 at 01:46PM by gi4c0
No comments:
Post a Comment