LoginSignup
1
1

More than 3 years have passed since last update.

chromeで401レスポンスが返ってきたときの不具合

Last updated at Posted at 2020-03-27

背景

https://www.nnn.ed.nico/questions/14833
n予備校の教材の通りに実装を進めていたところ、このリンクと同じ状況に遭遇

問題

chromeに限り、以下のコードで表示されるはずの"ログアウトしました"という文とリンクが表示されない。

 function handleLogout(req, res) {
   res.writeHead(401, {
    'Content-Type': 'text/html; charset=utf-8'
   });

   res.end('<!DOCTYPE html><html lang="ja"><body>' +
     '<h1>ログアウトしました</h1>' +
     '<a href="/posts">ログイン</a>' +
     '</body></html>'
   );
 }

原因

chromeのバグ
https://bugs.chromium.org/p/chromium/issues/detail?id=992639

解決

  • とりあえず別のステータスコードを変える

function handleLogout(req, res) {
  console.log('handleLogout');
  res.writeHead(400, {
    'Content-Type': 'text/html; charset=utf-8'
  });
  res.end('<!DOCTYPE html>'+
   '<html lang="ja"><body>' +
    '<h1>ログアウトしました</h1>' +
    '<a href="/posts">ログイン</a>' +
    '</body></html>');
}
  • (ブラウザを変える)

追記

教材にこのエラーについての解決方法が追記されていた。
解決方法は、ステータスコードを302に書き換えること。

https://developer.mozilla.org/ja/docs/Web/HTTP/Status
より、

302 Found
このレスポンスコードは、リクエストされたリソースの URI が一時的に変更されたことを示します。

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