无法在 nestJs 中启用 cors

cannot enable cors in nestJs

我正在尝试连接 React 和 NestJS 但出现 cors 错误 我从浏览器 (google chrome) 发出的请求被 cors 阻止了。 我什至无法记录传入的请求!!!!!!! 在浏览器网络选项卡中,我看到来自 xhr 的 CORS 错误 在控制台选项卡中我看到

Access to XMLHttpRequest at 'localhost:3500/api/v1/users/login' from origin
'http://localhost:3001' has been blocked by CORS policy: Cross origin requests are only supported
for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
Axios Error: Network Error
    at createError (createError.js:16:1)
    at XMLHttpRequest.handleError (xhr.js:84:1)
xhr.js:177 POST localhost:3500/api/v1/users/login net::ERR_FAILED

我试过{ cors: true }app.enableCors();

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
  });

const corsOptions: CorsOptions = {
    allowedHeaders: ['origin', 'x-requested-with', 'content-type', 'accept', 'authorization'],
    credentials: true,
    origin: ['http://localhost:3001', 'http://localhost:3000'],
  }
  app.enableCors(corsOptions);

看起来您正在向 localhost:3500/api/v1/users/login 发出请求,该请求没有方案。

改用http://localhost:3500/api/v1/users/login