LoginSignup
1
1

More than 1 year has passed since last update.

NestJSのコントローラーのレスポンスにHTTPステータスコードを追加する

Posted at

@Resを使うとステータスコードを追加できる

上記サイトより引用

We can use the library-specific (e.g., Express) response object, which can be injected using the @Res() decorator in the method handler signature (e.g., findAll(@Res() response)). With this approach, you have the ability to use the native response handling methods exposed by that object. For example, with Express, you can construct responses using code like response.status(200).send().

ライブラリ固有の(Expressなど)レスポンスオブジェクトを使用できます。これは、メソッドハンドラシグニチャの中で@Res()デコレータ(findAll(@Res()応答)など)を使用して挿入できます。このアプローチでは、そのオブジェクトによって公開されているネイティブのレスポンス処理メソッドを使用できます。たとえば、Expressでは、response.status(200).send()のようなコードを使用して応答を作成できます。

実際のコードの簡単な例

import {Response} from "express";

@POST("findList")
async findList(@Res() res :Response){
res.status(401).json({message:"you are not allowed to call this api"});
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1