【发布时间】:2021-11-18 18:22:30
【问题描述】:
我正在尝试使用带有 graphql api 的 nodejs 连接我的 Angular 应用程序,并且我正在使用 cookie/会话进行身份验证。
该 api 与 GraphQL PLayground 完美配合。 这是我与 graphql api 连接的角度连接。
const uri = "http://localhost:4000/graphql/"; // <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
return {
link: httpLink.create({ uri, withCredentials: true }),
cache: new InMemoryCache(),
};
}
如果我设置了withCredentials:false,连接就会建立,但我不能再使用cookies进行身份验证了。
这是我在 cors 中间件上的代码。
const corsOptions = {
credentials: true,
origin: "http://localhost:4200",
};
app.use(cors(corsOptions));
我已经尝试了以下代码 sn-ps 来完成这项工作,但没有成功。
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept",
);
res.header("Access-Control-Allow-Credentials", "true");
next();
});
const corsOptions = {
credentials: true,
origin: true,
};
app.use(cors(corsOptions));
我已尝试以下问题链接中的所有解决方案。
相关问题:
- CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
- Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
...和类似的帖子,但没有解决这个问题。
【问题讨论】:
标签: node.js angular express cors session-cookies