[英]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.