-3

I understand Mix-ins are to extend a common functionality across services. But Im not able to understand how middleware works in molecular and what problems It could help me solve.

Icebob
  • 1,132
  • 7
  • 14

1 Answers1

0

Check the documentation how middleware works in Moleculer framework: https://moleculer.services/docs/0.13/middlewares.html

With middlewares you can extend the framework functionality with your custom logic. Here is an example how a middleware looks like which extend service action handling:

const MyCustomMiddleware = {
    // Wrap local action handlers (legacy middleware handler)
    localAction(next, action) {
        return function(ctx) {
            // Change context properties or something
            return next(ctx)
                .then(res => {
                    // Do something with the response
                    return res;
                })
                .catch(err => {
                    // Handle error or throw further
                    throw err;
                });
        }
    }
};
Icebob
  • 1,132
  • 7
  • 14
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – double-beep Jun 16 '19 at 11:38