Relative Content

Tag Archive for node.jsexpressmodel-view-controller

Where to handle errors caused by user. In middleware or in service?

I am working on an web application using node + express on the backend. My rest api exposes 3 collections. One main collection accounts and 2 child collections goals and cashflows. On each collection there are CRUD operations available. For example when I want to find all goals I have to make following request: GET /api/v1/accounts/:account-id/goals. It becomes clear that I have to check if the account with given id exists before performing any actions on child collections. Where should I put that check? In middleware or in a service?. When designing my app I used 3 layers design pattern. Every request is received by route files and they forward them to the right controller. Controller is responsible for receiving the request and sending a response. To fulfill request it calls a service which is responsible for business logic and accessing database through the model.
One options is to put code that checks if account exists in the service, but if so I would have to paste that code multiple times in diffrent services. Another way is to put that in middleware but then it would have to access model layer to get data from db which I think only service should do. I’m not pasting any code but I will if needed. Any suggestions?

Express router and controllers in different files

I am considering using Express as backend framework for new full stack application I develop.
The application is going to be quite big, and I would like to keep the logic separated in different controllers (following the MVC design pattern).