cors中间件做什么?

use*_*422 3 node.js cors

正在查看一些创建了一些Web API的节点js代码并且遇到了这个:

//CORS Middleware
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
contentType,Content-Type, Accept, Authorization");
next();
});
Run Code Online (Sandbox Code Playgroud)

在互联网上环顾四周,似乎无法理解它的作用?有人可以解释一下cors中间件的目的

小智 9

此链接可能有所帮助.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

CORS允许您配置Web API的安全性.它与允许其他域对您的Web API发出请求有关.例如,如果您在一台服务器上安装了您的Web API,而在另一台服务器上安装了您的Web应用程序,则可以在Web API中配置CORS,以允许您的Web应用程序调用您的Web API.