First microservice handler
For our first microservice handler, let’s convert the UserController index method into a microservice handler. To do this, we copy the method and make a few simple modifications. Instead of annotating the method with Get
, we will use MessagePattern
.
@
Controller
()
export
class
UserController
{
@
Get
(
'users'
)
public
async
index
(
@
Res
()
res
)
{
const
users
=
await
this
.
userService
.
findAll
();
return
res
.
status
(
HttpStatus
.
OK
).
json
(
users
);
}
@
MessagePattern
({
cmd
:
'users.index'
})
public
async
rpcIndex() {
const
users
=
await
this
.
userService
.
findAll
();
return
users
;
}
}
A message pattern provides Nest.js the means for determining which microservice handler to execute. The pattern can be a simple string or a complex object. When a new microservice message is sent, Nest.js will search through all registered microservice handlers to find the handler that matches the message pattern exactly...