简体   繁体   English

为什么Chrome删除localhost:80的端口号?

[英]Why does Chrome remove port number for localhost:80?

I'm making a request from my frontend React application and running it on my server with localhost:80. 我正从我的前端React应用程序发出请求,并使用localhost:80在我的服务器上运行它。 When viewing the request and header in the browser, Chrome automatically removes the port number which causes a CORS error. 在浏览器中查看请求和标题时,Chrome会自动删除导致CORS错误的端口号。 Why does Chrome strip out this port number? 为什么Chrome会删除此端口号? What are best practices in this scenario? 这种情况下的最佳做法是什么? I've simply changed the port from 80 to 8080 and that resolved the issue. 我只是将端口从80改为8080,这就解决了这个问题。

I've set the headers to Access-Control-Allow-Origin for all types of HTTP requests and this did not seem to resolve the issue. 我已经为所有类型的HTTP请求将标头设置为Access-Control-Allow-Origin,这似乎无法解决问题。 This also led me to believe that the port being absent on the request header was the root cause of the issue (which is why I changed it from 80 to 8080). 这也让我相信请求头上没有端口是问题的根本原因(这就是为什么我将它从80改为8080)。

app.get('*', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // If needed
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // If needed
    res.setHeader('Access-Control-Allow-Credentials', true);
}

not working port here 这里没有工作的港口

app.listen(80, err => {
  if (err) {
    return console.error(err);
  }
  console.log(

      =====================================================
      -> Server (${chalk.bgBlue('Hot reload')}) 🏃 (running) on ${chalk.green(
      'localhost',
    )}:${chalk.green('80')}
      =====================================================
    ,
  );

^^this one was not working and chrome removed the port in the URL ^^这个没有用,chrome删除了URL中的端口

changed port here 在这里换了港口

app.listen(8080, err => {
  if (err) {
    return console.error(err);
  }
  console.log(

      =====================================================
      -> Server (${chalk.bgBlue('Hot reload')}) 🏃 (running) on ${chalk.green(
      'localhost',
    )}:${chalk.green('8080')}
      =====================================================
    ,
  );

^^this one was working and chrome left the port in the URL ^^这个工作正常,chrome离开了URL中的端口

I was expecting CORS to allow the requests to go through even when no port was specified but this did not happen. 我期待CORS允许请求通过,即使没有指定端口,但这没有发生。 I'm running a node.js server, utilizing graphql for my backend, react framework for my front end, and cors version is "cors": "^2.8.4" 我正在运行一个node.js服务器,使用graphql作为我的后端,为我的前端做反应框架,而cors版本是“cors”:“^ 2.8.4”

Port 80 is the default port already used by the world wide web (www). 端口80是万维网(www)已经使用的默认端口。 Is there a reason that this port number needs to be specifically used (for local development im guessing)? 是否需要专门使用此端口号(对于本地开发我猜)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的本地主机服务器不接受此端口号? - Why this port number is not acceptable for my localhost server? 为什么使用端口80在node.js中给出错误? - Why does using port 80 give error in node.js? 通过NGINX(localhost)在端口80上运行Ghost - Running Ghost on port 80 through NGINX (localhost) 服务器可以在localhost上运行,但不能在端口80上使用ip - Server works on localhost but not with ip on port 80 为什么我的应用程序在端口80而不是端口3000上侦听,因为我将其设置为在docker容器内运行? - Why does my app listen on port 80 instead of port 3000 as I set it running inside docker container? 为什么servername:port不起作用为什么node.js中的localhost:port起作用? - why servername:port does not work why localhost:port in node.js works? debian端口80不接受远程连接 - debian port 80 does not accept remote connections 为什么 axios 试图连接端口 80 而不是 8080? - Why is axios trying to connect on port 80 and not 8080? 如何在端口80上将node.js本地主机与端口90上的apache mysql连接起来? - How to connect node.js localhost at port 80 with apache mysql at port 90? socket.io服务websockets需要物理上在端口80上吗? - does socket.io serving websockets need to physically be on port 80?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM