Moleculer Routing
A short introduction
In this article, we will explore a few ways of setting up API service endpoints when using Moleculer API Gateway. If you don’t know what’s Moleculer API Gateway, please check the official website below.
Moleculer provides a few ways of defining API routes and depending on each project needs, you would like to choose between the following:
You can check a live version of the examples presented in this article in the code sandbox below:
1. Default Routes
The default routes allow you to write a service API without too many concerns. It uses the API Base Path and Action Names to expose the endpoints to the outside world.
When to use
Use this for quick prototypes, and when the methods the API’s resources are accessed isn’t important. With the default configuration, all the endpoints (called Actions in Moleculer) are available to any kind of HTTP requests (like GET, POST, UPDATE, DELETE etc.)
How to use
No extra configuration needed.
- Route configuration
- Greeter service
- Route examples
GET /simple/hello
POST /simple/hello
GET /simple/welcome?name=Razvan
POST /simple/welcome
Body { "name": "Razvan" }
2. Auto Aliases
This is one of the most powerful and interesting ways of creating RESTful APIs with Moleculer. Auto-aliases generator feature provides services with full flexibility to define a REST interface.
When to use
With Auto-Aliases, all the routes are specified inside services themselves, and this is great when you need to keep the services as loosely coupled as possible.
How to use
// Auto generate aliases
autoAliases: true
- Route configuration
- Users Service
- Route Examples
GET /autoalias/users
POST /autoalias/users
Body { "name": "Razvan Predescu" }
GET /autoalias/users/11
PUT /autoalias/users/11
Body { "name": "Razvan Daniel Predescu" }
DELETE /autoalias/users/11
3. RESTful Aliases
This is a variation of the Auto Aliases method above. The difference is that we define the route aliases into the API configuration, instead of services.
When to use
Use this configuration to create classic RESTful APIs when defining routes outside the services themselves is a better fit.
How to use
// RESTful aliases
aliases: {
"GET articles": "articles.list",
"GET articles/:id": "articles.get",
"POST articles": "articles.create",
"PUT articles/:id": "articles.update",
"DELETE articles/:id": "articles.remove"
}
- Route configuration
- Articles Service
- Route Examples
GET /rest/articles
POST /rest/articles
Body { "title": "New article via REST" }
GET /rest/articles/11
PUT /rest/articles/11
Body { "title": "Modified article title" }
DELETE /rest/articles/11
4. Shorthand RESTful Aliases
This is a variation of the RESTful Aliases method above. The difference is that instead of defining each route alias, we tell the API to generate all the routes for us.
When to use
Use this method to create classic RESTful APIs while writing minimum configuration into the API service.
How to use
// Shorthand RESTful aliases
aliases: {
"REST articles": "articles"
}
- Route configuration
- Articles Service
- Route Examples
GET /shortrest/articles
POST /shortrest/articles
Body { "title": "New article via REST" }
GET /shortrest/articles/11
PUT /shortrest/articles/11
Body { "title": "Modified article title" }
DELETE /shortrest/articles/11
Summary
In this article, we covered a few ways of defining API service endpoints in Moleculer. Choose the most appropriate that makes sense for your projects and don’t be shy to even mix them together as multiple route definitions within the same API configuration are allowed.
Please check the official Moleculer documentation, and discover all the features this beautiful framework provides.
Best!