In this tutorial we'll develop a simply Node.js microservice that expose a route through the Mia-Platform API Gateway.
In particular calling the route we'll get a response in base of the value of an environment variable. In this example we'll creating a CRUD for storing customers and orders with a set of orders and develop a Microservice that calculate the shipping cost for a specific order. If the order belong to a new customer the shipping it's free, otherwise it's calculate from a fixed value.
Prepare your favourite IDE to interact with Git repositories.
First of all open your project, and create the following MongoDB CRUD collections. In order to do this follow these steps:
- In the Console select your project.
- Select Design section.
- Select the branch master.
- Go to section MongoDB CRUD and click Create new CRUD.
- Insert the name
customersand press Create.
- Now you have to set your collection's fields. Scroll to Fields section and click Add New to create the following fields:
orders CRUD with the following fields:
|orderItems||Array of Objects|
Now you should expose your collection through an Endpoint and create your first API.
Go to section Endpoints and click Create new endpoint. Then enter the following information:
/customersas Base path.
Click Create. You have just created your first API! The detail view shows other configurations that we'll use later. Scroll down to Routes section to see an overview of routes of your API.
Create a new endpoint with the following information:
/ordersas Base path.
- To confirm changes just press Commit & Generate. We suggest to write a proper title for each commit, in this case, we can enter
Created CRUDs "customer","orders" and the related endpoints.
All items in design area are not saved until you click Commit & Generate. However, you can create different entities and then save all the work only at the end.
Internally, when you commit, the Console properly updates and commit the Kubernetes files on configuration repository of your project.
Checkout the endpoint documentation for more information about the section and to discover all the potentialities of this feature.
Watch this short video to learn how it's easy to create a Node.js Microservice from templates
A Microservice can be created starting from existing Templates. Starting from a Mia-Platform Template or using Mia-Platform Service Libraries has many advantages and allows your microservice to be compliant with all best practices(such as testing, logging, health routes, etc).
In the Marketplace you can find a list of Examples or Templates powered and supported by Mia-Platform that allows you to set-up microservices with a tested and pre-defined function.
We'll create a microservice from Node.js template:
- Go to section Microservices section and click Create a Microservice.
- Search the template
Node.js Template, then click the related tab.
- Now enter the following information:
- Select the suggested option as Docker Image Name.
- Commit the changes