nest icon indicating copy to clipboard operation
nest copied to clipboard

@Get() not working without parameter

Open basvdijk opened this issue 8 years ago β€’ 10 comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When using the code below inside the @controller('cats')

  @Get()
  getCats(): object {
    return { res: true};
  }

I got a 404 when visiting http://localhost:3000/cats/getCats However when I change the code to:

  @Get('getCats')
  getCats(): object {
    return { res: true};
  }

It works. According to the documentation I shouldn't have to give an explicit name to @Get

Expected behavior

To have @Get() working without parameters.

Environment


Nest version: 5.0.0
 
For Tooling issues:
- Node version: 9.9.0
- Platform:  Mac

Others:

basvdijk avatar Jun 08 '18 06:06 basvdijk

@basvdijk this code that you show works, but not in the way that you're thinking it's.

Which mapping like this:

  @Get()
  getCats(): object {
    return { res: true};
  }

with @controller('cats') above class definition, the correct URL mapping is:

http://localhost:3000/cats

not

http://localhost:3000/cats/getCats

Idk from where you get that it will be the second example.

cojack avatar Jun 08 '18 06:06 cojack

Hi, the problem is the route that you call, if you don’t pass any parameter to the get decorator, then you have to call the route as GET /cats instead of GET /cats/getCats.

adrien2p avatar Jun 08 '18 06:06 adrien2p

Ah now I get it. Wouldn't it be an idea to have @Get('/') for the root e.g. http://localhost:3000/cats and that:

  @Get()
  getCats(): object {
    return { res: true};
  }

Automatically maps to /cats/getCats ?

basvdijk avatar Jun 08 '18 07:06 basvdijk

i don't think that link the name of the method and the route path should be a good idea, you could want to have any method name that you want without impact the path route.

adrien2p avatar Jun 08 '18 07:06 adrien2p

@basvdijk exactly what @adrien2p wrote, I agree with him at 100%. Especially for REST controllers.

cojack avatar Jun 08 '18 07:06 cojack

Hey guys this a discussion about the URI design.

I think put the HTTP verb in the URI path is a very very very very... (stop writing very) bad behaviour or a URI misunderstanding.

When you expose resources from a server you don't want to know what is the code method name behind the resource.

REST API is not an extension of POO. In fact if there is a mapping between the HTTP verb and method name we could also add class name in the URI path. You could have a fully descriptive Cats object with your URI. http://localhost:3000/CatsController/getCats in your browser search bar or in your developer tools network sounds very bad !

thomrick avatar Jun 08 '18 07:06 thomrick

Totally agree with @ThomRick, avoid Request Method in the path naming

adrien2p avatar Jun 08 '18 07:06 adrien2p

Thanks for your thoughts, was just a pop of mind. Your feedback clarified enough why this is a bad idea :)

basvdijk avatar Jun 08 '18 09:06 basvdijk

i think this is a good definition of a restful api @basvdijk

A RESTful API explicitly takes advantage of HTTP methodologies defined by the RFC 2616 protocol.
They use GET to retrieve a resource; PUT to change the state of or update a resource, which can be an object, file or block; POST to create that resource; and DELETE to remove it.

I would additional add that on an empty resource like:

POST /api/v1/user

the API can create a resource and with an resource ID can be updated with

POST /api/v1/user/{userId}

https://searchmicroservices.techtarget.com/definition/RESTful-API

FrancisVarga avatar Jun 11 '18 17:06 FrancisVarga

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 24 '19 21:09 lock[bot]