LoginSignup
0
0

More than 3 years have passed since last update.

corrs について 猿でもわかる

Posted at

バックエンド側でnode,expressを触っている人向け

CORRS とは、API通信で値(param)など、バックエンド側のデータを表示させるために
バックエンド側で設定するための物です。

なので、今回は設定方法だけ記載します。

これを書いておけばHTTP通信は正常に行われます。

バックエンド側に
app.use((req, res, next) => {
// res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Headers", "Origin, Authorization, Accept, Content-Type");
// res.header("Access-Control-Allow-Headers", "*");
res.header('Access-Control-Allow-Credentials', "true");

// OPTIONSリクエスト(プリフライトリクエスト)への応答
// 参考: https://developer.mozilla.org/ja/docs/Web/HTTP/CORS#preflighted_requests
if (req.method == "OPTIONS") {
    res.send(200);
} else {
    next();
}

});

今回はAngularを使っている方向けです。

指摘がありましたら、コメント等お願いします。

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