gpt4 book ai didi

javascript - Cors 似乎在我的 Node 服务器上默认启用。我错过了什么吗?

转载 作者:行者123 更新时间:2023-12-01 03:18:17 25 4
gpt4 key购买 nike

所以我有一个非常基本的 Node 和 Express 服务器,如下所示。没有中间件或任何东西,但我仍然能够记录 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');
});

最佳答案

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

This document defines a mechanism to enable client-side cross-origin requests.

及以后

User agents commonly apply same-origin restrictions to network requests. These restrictions prevent a client-side Web application running from one origin from obtaining data retrieved from another origin, and also limit unsafe HTTP requests that can be automatically launched toward destinations that differ from the running application's origin.

(我的重点)

关于javascript - Cors 似乎在我的 Node 服务器上默认启用。我错过了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45390097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com