Node express 服务器,CORS API 限制,包括我开发机器的主机文件中的一个条目

Node express server, CORS API restriction, including an entry in my development machine’s hosts file

我正在尝试使用具有 API CORS 策略的 API,该策略不支持来自任何域的浏览器请求。为了允许客户端 JavaScript 代码访问 API,在开发我的应用程序时,有人建议我从“*.thisCompany.com”域提供我的 Web 应用程序。 建议在我的开发机器的主机文件中包含一个条目,如下所示,我已经这样做了:

$ echo '127.0.0.1 localhost.thisCompany.com' >> /etc/hosts

当我 运行 sudo nano /private/etc/hosts 时遵循此命令 这是我看到的画面。

Host Database

localhost is used to configure the loopback interface
when the system is booting.  Do not change this entry.
127.0.0.1       localhost
127.0.0.1       localhost.thisCompany.com

然后我被告知我应该可以在 http://localhost.thisCompany.com 访问我的网络应用程序。

我正在使用 node express 作为我的服务器,我的 server.js 文件中的代码如下所示

var express = require('express');
var server = express();
var path = require('path');

var port = process.env.PORT || 'thisCompany.com';

server.use(express.static(__dirname + '/public'));
server.use('/bower_components', express.static(__dirname + '/bower_components'));

  server.get('/', function(request, response) {
    response.sendFile(path.join(__dirname + '/views/index.html'));
  });

  server.listen(port, function() {
    console.log("Node app is running at localhost:" + port)
  });

谁能告诉我应该遵循哪些步骤才能调用此 API 并绕过 API CORS 策略?

我在这里阅读了各种其他帖子,也阅读了其他在线文章,但是我找不到解决方案。真的希望这方面的人能提供帮助。

谢谢,保罗

我误解了我应该如何在我的本地计算机上修改主机文件后在我的计算机上本地查看页面。我不需要向我的快速服务器添加任何东西。快递服务器代码保持如下:

var express = require('express');
var server = express();
var path = require('path');

var port = process.env.PORT || 3000;

server.use(express.static(__dirname + '/public'));
server.use('/bower_components', express.static(__dirname + '/bower_components'));

  server.get('/', function(request, response) {
    response.sendFile(path.join(__dirname + '/views/index.html'));
  });

  server.listen(port, function() {
    console.log("Node app is running at localhost:" + port)
  });

将我原来 post 中提到的行添加到我本地机器上的主机文件后,我需要启动我的服务器并按照此 link.[=12 访问该页面=]

http://localhost.thisCompany.com:3000/

虽然这是一个小众问题,但我希望这 post 对以后的人有所帮助。