LoginSignup
0
0

More than 3 years have passed since last update.

【Express】res.send()メソッドが実行済みか判定する

Last updated at Posted at 2021-04-27

res.send()メソッドは1回だけ実行できる。2回目はエラーになる。

res.send('1');
res.send('2'); // Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

res.send()メソッドが実行済みか判定するにはres.writableEndedプロパティを使う。

consle.log(res.writableEnded) // false

res.send('1');

consle.log(res.writableEnded) // true

if (!res.writableEnded) {
  res.send('2'); // 実行されない
}

参考
https://nodejs.org/api/http.html#http_response_writableended

0
0
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
0
0