默认情况下,Cors 似乎在我的节点服务器上处于启用状态.我错过了什么吗?



所以我有一个非常基本的节点和快速服务器,如下所示。没有中间件或任何东西,但我仍然能够记录 axios 对 google.com 的响应。这不应该导致cors错误,导致我不得不使用中间件或设置服务器进行跨源请求吗?对CORS仍然有点不清楚,任何澄清都非常感谢。谢谢!

var express = require('express');
var app = express();
var axios = require('axios');
app.on('listening', () => {
axios.get('https://google.com')
.then(response => console.log(response.data));
});
app.get('*', function(request, response){
response.sendfile('./dist/index.html');
});
app.use(function(err, req, res, next) {
if (err) {
res.status(500).send(err);
console.log(err.message);
}
next();
});
app.listen(process.env.PORT || 3000, () => {
app.emit('listening');
console.log('listening on 3000');
});

CORS是一个浏览器的东西。它不适用于服务器端代码(在Node,PHP,Java等中(。从规格:

本文档定义了启用客户端跨源请求的机制。

及以后

用户代理通常对网络请求应用同源限制。这些限制可防止从一个源运行的客户端 Web 应用程序获取从另一个源检索的数据,还可以限制不安全的 HTTP 请求,这些请求可以自动启动到与正在运行的应用程序的源不同的目标。

(我的强调(

最新更新