Installation
$ npm install moleculer --save
or
$ yarn add moleculer
Create your first microservice
This example shows you how to create a small service with an add action which can add two numbers and how to call it.
const { ServiceBroker } = require("moleculer");
// Create a broker
let broker = new ServiceBroker({ logger: console });
// Create a service
broker.createService({
name: "math",
actions: {
add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
}
}
});
// Start broker
broker.start()
// Call service
.then(() => broker.call("math.add", { a: 5, b: 3 }))
.then(res => console.log("5 + 3 =", res))
.catch(err => console.error(`Error occurred! ${err.message}`));
网友评论