【发布时间】:2018-01-05 12:05:03
【问题描述】:
所以我有一个非常基本的节点和快速服务器,如下所示。没有中间件或任何东西,但我仍然能够记录 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');
});
【问题讨论】:
-
您的服务器向其他服务器请求不是跨源请求
-
跟进@ArpitSolanki 所说的,跨域策略是Web 浏览器 的一项安全功能,计算机可以并且确实通过http 从互联网请求资源。时间。想想
curl、wget等。在您的服务器上运行的代码没有“来源”,因此对另一个资源的请求不能是“跨来源”。
标签: javascript node.js express cors