Create your first microservice

This example shows you how to create a small service with an add action which can add two numbers.

const { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({ logger: console });
name: "math",
actions: {
add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
// Call service
.then(() =>"math.add", { a: 5, b: 3 }))
.then(res => console.log("5 + 3 =", res))
.catch(err => console.error(`Error occured! ${err.message}`));
Try it in your browser!

Open this example on Runkit!

Create a Moleculer project

Use the Moleculer CLI tool to create a new Moleculer based microservices project.

  1. Install moleculer-cli globally

    $ npm install moleculer-cli -g
  2. Create a new project (named first-demo)

    $ moleculer init project-simple first-demo

    Press Y to all questions

  3. Open project folder

    $ cd first-demo
  1. Start project

    $ npm run dev
  2. Open the http://localhost:3000/greeter/hello link in your browser. It will call the hello action of greeter service via API gateway and will return with the result.


You created your first Moleculer based microservices project! The next step is to check our examples or demo projects.