简体   繁体   中英

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. When viewing the request and header in the browser, Chrome automatically removes the port number which causes a CORS error. Why does Chrome strip out this port number? What are best practices in this scenario? I've simply changed the port from 80 to 8080 and that resolved the issue.

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. 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).

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

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

I was expecting CORS to allow the requests to go through even when no port was specified but this did not happen. 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"

Port 80 is the default port already used by the world wide web (www). Is there a reason that this port number needs to be specifically used (for local development im guessing)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM