Nest: @Get() not working without parameter

Created on 8 Jun 2018  路  10Comments  路  Source: nestjs/nest

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:

discussion 馃敟

Most helpful comment

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 !

All 10 comments

@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.

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

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 ?

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.

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

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 !

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

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

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

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.

Was this page helpful?
0 / 5 - 0 ratings