5 easy steps to create your REST microservice in NodeJS

Icebob
Icebob
Aug 23, 2017 · 4 min read

Do you like NodeJS? Are you interested in microservices but don’t know how to create them? Stay with me! I will show you how easy it is to create a microservice with the Moleculer microservices framework in NodeJS.

What are we going to do?

We are going to create a microservice, which can store data in a memory DB and publish access via HTTP port (API Gateway) with REST API.

What you need is…

Are you ready?

Step 1: Install Moleculer CLI tool

We are going to create a Moleculer project from a template. For this, we use the official Moleculer CLI tool. You can install it with the following command:

npm install -g moleculer-cli

Step 2: Create a new microservice project

moleculer init project-simple my-first-service

This command creates a new Moleculer-based project to the my-first-service folder. It will ask the followings:

Give “No” answer for the second question to avoid installing a message broker (e.g. NATS).

If the command is executed there will be a my-first-service folder containing a NodeJS project prepared to the Moleculer framework.

Generated project files

Step into this folder with cd my-first-service command.

Step 3: Install Moleculer DB module

We use the moleculer-db module as a mixin to store data in our service.

Install it with the following command:

npm install moleculer-db --save

The moleculer-db is CRUD database access service. It is a simple solution. If you need more, just extend it with custom mixins.

Step 4: Create a new service

Open the created folder in your text editor. It has a services folder containing the services. The api.service.js is the API gateway service. It will publish your new one. The greeter.service.js is just an example service.

Make a products.service.js file in the services folder and paste the following code to the file to create a new service to store products:

The moleculer-db module is a universal data store module. It has common CRUD actions (create, list, find, get, update, remove). If you don’t set an adapter it will use the built-in NeDB adapter and will store data in the memory.

Start your project with the npm run dev command. It will publish your service on the port 3000 (default). Open the http://localhost:3000/api/products/list URL in the Postman and you’ll get a response from your service.

Result in Postman

Congratulations, you have created your REST microservice!

Once the services started, the console will switch to REPL mode. In REPL mode you can get information about your services, call them, emit events …etc. You can read more about REPL commands here.

With npm run dev command the broker switches to hot reloading mode, so you can modify & test your services without restarting.

Press the q key to exit from REPL mode and stop the services.

Step 5: Configure REST in the API Gateway service

To access the new product service via REST API open the api.service.js file and modify it according to the following code (add thealiases block):

The "REST products" key in aliases creates the common REST paths and links them to the products service actions.

"GET products": "products.list",
"GET products/:id": "products.get",
"POST products": "products.create",
"PUT products/:id": "products.update",
"PATCH products/:id": "products.patch",
"DELETE products/:id": "products.remove"

You can read more about aliases in Moleculer Web documentation.

Save changes and the service will be hot reloaded. Now you can test your service with Postman.

  • Create a product:POST http://localhost:3000/api/products
  • Get a product by ID:GET http://localhost:3000/api/products/12345
  • List products:GET http://localhost:3000/api/products
  • Update a product:PUT http://localhost:3000/api/products/12345
  • Delete a product:DELETE http://localhost:3000/api/products/12345
Test collection for Postman

Conclusion

You have created your first microservice successfully. Okay, I know that it was a very simple demo. It doesn’t contain persistent store, authentication, authorization, scaling …etc. But you can see how easy it is to create a basic microservices with the Moleculer framework which is able to store your data and you can access it via REST API.

✨ Bonus steps

Store data in MongoDB

There is an official MongoDB adapter for the Moleculer DB module. You can set it in your service and your data will be stored in a MongoDB collection.

Install the adapter:

npm install moleculer-db-adapter-mongo --save

Set up adapter into your service:

It’s done. From now every product will be stored in your MongoDB.


If you want to learn more about Moleculer framework, check out our website https://moleculer.services or Github.

Follow us on Twitter at @MoleculerJS or join to our Gitter chat.

Moleculer

Official blog for Moleculer framework

)

Icebob

Written by

Icebob

A full-stack javascript developer, founder of Moleculer

Moleculer

Moleculer

Official blog for Moleculer framework

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade