Middleware or controller?

Hey everyone,

We have a team discussion which approach is better in terms of Best Practices. The current approach was to use route middleware to populate data and assign data to ctx.response.body like this:

export default factories.createCoreRouter('api::test:test', {
only: ['find'],
config: {
find: {
  middlewares: [
  async (ctx, next) => {
  await next();
  if (!ctx.url.startsWith('/api')) {
  const entry = await strapi.entityService.findMany('api::test.test', {
  locale: ctx.qeury.locale,
  fields: ['test'],
  populate: {//etc.....}
 //some additional business logic/feature flags
ctx.response.body = entry;

New proposal is to move everything from route middlewares to controllers and keep it there (data population, business logic, etc).
What’s your take on that? Which approach is better? What do you middlewares for?

This topic has been created from a Discord post (1227594353032171601) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

Anyone? Seeking for help :eyes:

If you use your populate solely on that controller I would do that in controller

If there is multiple controllers need same populate then middleware is better option

However you can also write a service and apply populate in there and get data from service in the controller

Thank you for the response!