0

I am developing an API backend in NestJS, now for some specific routes (or controllers) I am loading in a configuration from the database to handle an incoming request. This fetching of the configuration is done inside my service (business logic), but I was wondering that perhaps this can be separated using middleware. Idea is that I load in the configuration in the middleware and then pass it on to the service. To go even further I was thinking of checking preconditions with the incoming request and the configuration, in order to determine if the request should be passed on to the service.

I mostly see middleware being used for authentication and object validation preconditions, so I was not sure if the above would also be a right fit.

To give some further context, the configuration is request/user dependent and is not global.

So in short: Is this a proper usage of middleware? Or should the above be part of the business logic? If it should be part of the business logic, what would be a good way to separate the checking of preconditions with the actual logic itself?

Best regards

1 Answer 1

0

In my opinion, you are right. The middleware should handle business logic alone.

It seems to me that the validation mechanism you have is considerably heavy. Hence, it is not uncommon to have a separate validation layer (a component or a service) between the user request and middleware. Separation of concerns is better implemented in this way and the api stack is loosely coupled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.