【发布时间】:2021-01-13 01:58:38
【问题描述】:
我正在学习服务器/客户端通信。 这是我服务器端代码的一部分。
import express from 'express';
import cors from 'cors';
const app = express();
app.use(cors({ credential: true, origin: process.env.CORS_ORIGIN }));
我尝试使用 Chrome 浏览器与服务器通信,但我的客户端 fetch() 函数抛出错误。
Access to fetch at 'http://localhost:4000/login' from origin 'http://localhost:8000' has been blocked
by CORS policy: Response to preflight request doesn't pass access control check: The value of the
'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the
request's credentials mode is 'include'.
难怪,因为我将 cors 属性作为“凭据”。它是“凭证”。 问题是,这件事上没有显示错误代码,所以我认为响应头有问题。
我花了将近一天的时间才发现是一个人的失踪导致了这个问题!!!!!
如果我更了解我导入的内容(cors 中间件),我可能会更好地了解在哪里查看。但我正在学习过程中。由于我的知识水平没有更好的线索,我不得不忍受很多小时才能最终指出这个错字。
我的问题是,我在这件事上是否遗漏了任何工具? 有没有什么办法可以更好地建议错别字或警告潜在问题,尤其是在进口了这么多中间件的情况下? 我知道有 ESLint,但它没有给我一个警告。
【问题讨论】:
标签: javascript json visual-studio-code middleware