Getting express server to accept CORS request

前端 未结 6 715
陌清茗
陌清茗 2020-12-06 09:42

I have my express server running on http://localhost:3000 (I call this web-server) I have another application running on localhost:8100 (I call this simply \'app\')

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 10:19

    Apparently cors module didn't work.

    Using the hints given above I used the following code:

      if (req.method === "OPTIONS") {
        res.header('Access-Control-Allow-Origin', req.headers.origin);
      } else {
        res.header('Access-Control-Allow-Origin', '*');
      }
    

    This did the trick.

提交回复
热议问题