【问题标题】:Cors seems to be enabled on my node server by default. Am I missing something?默认情况下,我的节点服务器上似乎启用了 Cors。我错过了什么吗?
【发布时间】: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 从互联网请求资源。时间。想想curlwget 等。在您的服务器上运行的代码没有“来源”,因此对另一个资源的请求不能是“跨来源”。

标签: javascript node.js express cors


【解决方案1】:

CORS 是一个浏览器的东西。它不适用于服务器端代码(在 Node、PHP、Java 中...)。来自the spec

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

以后

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

(我的重点)

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2013-10-17
    • 1970-01-01
    • 2022-11-17
    • 2014-11-18
    相关资源
    最近更新 更多