🐺
Nest.js の controller でルートパラメータをハイフンを入れると動かない
背景
ルートパラメータにはハイフンは使えないようです。エラーを出してくれないので謎に時間を溶かしてしまいました。戒めとしてメモしておきます。
失敗例
@Controller('example')
export default class ExampleController {
constructor(private readonly exampleService: ExampleService) {}
@Get('/:foo-value')
async find(
@Param('foo-value') fooValue: string,
): Promise<Example> {
return this.exampleService.example({ fooValue: fooValue })
}
}
これだと http://localhost:3000/example
に GET でアクセスしても 404 を返してきます。
正しい例
例えば、 value
とかで受け取っちゃいます。
@Controller('example')
export default class ExampleController {
constructor(private readonly exampleService: ExampleService) {}
@Get('/:value')
async find(
@Param('value') value: string,
): Promise<Example> {
return this.exampleService.example({ fooValue: value })
}
}
debugger で controller.ts
の実行したいメソッドの中にチェックポイントをつけて、そもそも中に入ることができているかも確かめることでバグに気づきやすくなります。
Discussion