Route parameters
Nest.js makes it easy to accept parameters from the route path. To do so, you simply specify route parameters in the path of the route as shown below.
import
{
Controller
,
Get
,
Param
}
from
'@nestjs/common'
;
@
Controller
(
'entries'
)
export
class
EntryController
{
@
Get
(
':entryId'
)
show
(
@
Param
()
params
)
{
const
entry
:Entry
=
this
.
entriesService
.
find
(
params
.
entryId
);
return
entry
;
}
}
The route path for the above handler method above is /entries/:entryId
, with the entries
portion coming from the controller router prefix and the :entryId
parameter denoted by a colon. The @Param()
decorator is used to inject the params object, which contains the parameter values.
Alternately, you can inject individual param values using the @Param()
decorator with the parameter named specified as shown here.
import
{
Controller
,
Get
,
Param
}
from
'@nestjs/common'
;
@
Controller
(
'entries'
)
export
class
EntryController...