moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.13k stars 580 forks source link

Ability to add middleware using broker config #165

Closed mailaneel closed 6 years ago

mailaneel commented 6 years ago

When using moleculer runner, there is no easy way to provide the middleware using suggested moleculer.config.js

module.exports = {
  nodeID: 'local-services',
  logger: true,
  logLevel: 'debug',
  middleware: [responseMiddleware()]
}

Without the above option, we still have to manually create broker and call .use for adding middleware.

icebob commented 6 years ago

Plus add created & started lifecycle handlers too. The created is called when the Runner created the ServiceBroker instance. The started is called after the broker.start().

icebob commented 6 years ago

Done. It will be released in v0.12

// moleculer.config.js
module.exports = {
    logger: true,

    // Add middlewares
    middlewares: [myMiddleware()],

    // Fired when the broker is created
    created(broker) {
    },

    // Fired when the broker is started
    started(broker) {
        // You can return with Promise
        return broker.Promise.resolve();
    },

    // Fired when the broker is stopped
    stopped(broker) {
        // You can return with Promise
        return broker.Promise.resolve();
    }
};