LoginSignup
0
0

More than 1 year has passed since last update.

【React Express】CORSエラー回避(特定のURLの許可)

Posted at

前回

前回このような記事をアップロードしました。
この時は許可するドメインを"*"としてすべて許可していました。
今回は特定のURLのみを許可する設定をしていきます。

やったこと

api.js
app.use(
  cors({
    origin: "http://example.net",
    credentials: true, //レスポンスヘッダーにAccess-Control-Allow-Credentials追加
    optionsSuccessStatus: 200, //レスポンスstatusを200に設定
  })
);

これだけです!
originに指定のURLを記述しました

つまづいた所

originに設定するURLはクライアント側なので、一度ブラウザで開いてそのURLをコピペしていました。その時のURLはhttp://example.net/と最後に/が入っていました。
そのため、何度試しても許可されず、時間を使ってしまいました。

ですので、気を付けましょう!

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