Skip to content

Commit

Permalink
docs(controllers) remove route order section
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 4, 2019
1 parent 974cdfa commit aac9231
Showing 1 changed file with 0 additions and 23 deletions.
23 changes: 0 additions & 23 deletions content/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,6 @@ findOne(id) {
}
```

#### Routes order

Be aware that route registration **order** (the order each route's method appears in a class) matters. Assume that you have a route that returns cats by identifier (`cats/:id`). If you register another endpoint **below it** in the class definition which returns all cats at once (`cats`), a `GET /cats` request will never hit that second handler as desired because all path parameters are optional. See the following example:

```typescript
@Controller('cats')
export class CatsController {
@Get(':id')
findOne(@Param('id') id: string) {
return `This action returns a #${id} cat`;
}

@Get()
findAll() {
// This endpoint will never get called
// because the "/cats" request is going
// to be captured by the "/cats/:id" route handler
}
}
```

In order to avoid such side-effects, simply move the `findAll()` declaration (including its decorator) above `findOne()`.

#### Scopes

For people coming from different programming language backgrounds, it might be unexpected to learn that in Nest, almost everything is shared across incoming requests. We have a connection pool to the database, singleton services with global state, etc. Remember that Node.js doesn't follow the request/response Multi-Threaded Stateless Model in which every request is processed by a separate thread. Hence, using singleton instances is fully **safe** for our applications.
Expand Down

0 comments on commit aac9231

Please sign in to comment.