0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Express.js でCORS設定をしているのにCORSエラーが出る場合の対処方法

Posted at

概要

Express + Vue.jsで開発をしていて、Express.jsでCORS設定をしたのにCORSエラーで怒られ続けて少しハマりました。

解決方法

optionsSuccessStatus: 200を設定することで解決しました。
公式ドキュメントでは204で設定していましたが、IEなどのレガシーブラウザでは 正常に動作しない可能性があるようなので200で返すようにして良さそうです。
公式ドキュメント : https://expressjs.com/en/resources/middleware/cors.html#configuration-options

app.use(cors({
    origin: 'http://localhost:5173', 
    credentials: true,
    optionsSuccessStatus: 200 // この行を入れないと駄目だった
}));
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?