我试图遵循本Awad完全堆栈编辑克隆教程。使用快速会话,我试图在客户端每次登录时发送一个cookie和userID --但是当我这样做时,cookie正在被设置,但是服务器停止了从客户机(阿波罗工作室或邮递员)的响应。如果删除cookie,服务器将返回正常工作。我已经把我的代码和其他人做了比较,却找不到任何错误。我还能错过什么?
import { MikroORM } from "@mikro-orm/core";
import microConfig from "./mikro-orm.config";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import { HelloResolver } from "./resolvers/hello";
import { PostResolver } from "./resolvers/Post";
import { UserResolver } from "./resolvers/User";
import {__prod__} from "./constants";
import {createClient} from "redis";
import session from "express-session";
import connectRedis from "connect-redis";
import "reflect-metadata";
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
const app = express();
const RedisStore = connectRedis(session);
const redisClient = createClient();
await redisClient.connect();
const appSession = session({
name: "cid",
secret: "shhhh",
resave: false,
saveUninitialized: false,
store: new RedisStore({
client: redisClient,
})
,
cookie: {
maxAge: 1000 * 60 * 60 * 24,
secure: true,
httpOnly: false,
sameSite: "none",
},
})
app.use(appSession);
!__prod__ && app.set("trust proxy", 1);
app.set("Access-Control-Allow-Origin", "https://studio.apollographql.com");
app.set("Access-Control-Allow-Credentials", true)
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, PostResolver, UserResolver],
validate: false,
}),
context: ({ res, req }) => ({ em: orm.em, res, req }),
introspection: !__prod__,
});
await apolloServer.start();
apolloServer.applyMiddleware({
app,
cors: {
origin: ["https://studio.apollographql.com"],
credentials: true,
},
});
app.get("/hello", (_, res) => {
res.send("Hello World");
});
app.listen(4000, () => {
console.log("server started on localhost:4000");
});
};
main().catch((err) => {
console.error(err);
})
发布于 2022-03-29 06:57:39
问题是,需要将legacyMode在createClient中设置为true,才能使redis与connect一起正常工作:)
https://stackoverflow.com/questions/71659906
复制相似问题