#How To -- Controller with Unique API Endpoints?

17 messages · Page 1 of 1 (latest)

crude veldt
#

Hello everyone! New to NestJS, and I've been looking at some tutorials and with the docs and had a question that I just wasn't able to answer. Is it possible to have unique API endpoints with NestJS using variables?

What I mean for this is for example, say I have an app like Google Drive or something and an endpoint looks like this:

GET /drives/{drive-id}/items/{item-id}

Is it possible for me to assign the values for {drive-id} and {item-id} in my NestJS controller? If so, could someone direct me to proper documentation or provide sample code? Thanks!

hybrid orchid
#

If i understand you correctly

You can add params to your endpoint path and get them in the controller function with @Param() decorator

@Get("/drives/:driveId/items/:itemId")
    getItem(@Param("driveId) driveId:string, @Param("itemId") itemId:string){
  //your code
}

crude veldt
#

Thank you ! This is exactly what I was looking for 😄

crude veldt
#

Is it possible to pass a parameter to Redirection as well?

#

Suppose I have a Drives Controller and an Items Controller

#

How would I be able to re-direct /drives/{drive-id} => /items/{item-id} Only if the items path and item id have been provided

#

@hybrid orchid

hybrid orchid
crude veldt
#

Hmm makes sense

hybrid orchid
#

if you really want to redirect request to another endpoint u can use res.redirect and specify url in your controller

#
@Get("/drives/:driveId/items/:itemId")
    getItem(@Param("driveId) driveId:string, @Param("itemId") itemId:string, @Res() res:Response){
   if(driveId && itemId) res.redirect(`/items/${itemId}`)
}

#

but i think it's better to create 2 separated endpoints

#

@crude veldt

crude veldt
#

Yeah that would probably easier as well

#

Thank you for the suggestion