Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8906

corrs について 猿でもわかる

$
0
0

バックエンド側で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を使っている方向けです。

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


Viewing all articles
Browse latest Browse all 8906

Trending Articles