gpt4 book ai didi

javascript - 如何在Node JS中使用Redis session 设置Heroku Redis

转载 作者:行者123 更新时间:2023-11-30 06:50:03 27 4
gpt4 key购买 nike

我正在努力让heroku redis在Node.js上工作...我创建了一个具有注册登录并查看计划的基本应用程序。我所有的终点都是邮递员,当我点击登录路径时,我会收到一个cookie。如下图所示。在本地,我在邮递员中获取cookie,并在我的应用程序浏览器中获取。

enter image description here
这是邮递员cookie的图像。

以下是我的开发代码片段。



const express = require("express");
const session = require("express-session");
const redis = require("redis");
const client = redis.createClient();
const redisStore = require("connect-redis")(session);
const cors = require("cors");
const cookieParser = require("cookie-parser");
const authRouter = require("../Auth/authRouter");
const plansRouter = require("../Plans/plansRouter");

// const redis = require("redis");
// if (process.env.NODE_ENV == "production") {
// client = redis.createClient(process.env.REDIS_URL);
// } else {
// client = redis.createClient();
// }

const server = express();
server.use(
cors({
credentials: true,
origin: "http://localhost:3000"
})
);
server.use(express.json());
server.use(cookieParser("this is my secret"));
server.use(
session({
secret: "this is my secret",
name: "sequelize",
unset: "destroy",
rolling: true,
credentials: "include",
cookie: { secure: false },
store: new redisStore({
host: "localhost",
port: 6379,
client: client,
ttl: 86400
}),
saveUninitialized: false,
resave: false
})
);
server.use("/auth", authRouter);
server.use("/plans", plansRouter);
console.log(client);

client.on("connect", () => {
console.log(`\n ** Redis client connected ** \n`);
});
client.on("error", function(err) {
console.log("Something went wrong " + err);
});

module.exports = server;





enter image description here
这是浏览器中cookie的图像。

这是我在本地运行Redis时的代码片段。



但是现在我想部署它,所以我选择使用heroku,但它不起作用。我阅读了heroku开发文档,但无法正常工作

因此,我添加了process.env.REDIS_URL,但不确定是否还有其他操作。我不确定我是否必须在商店中进行任何更改。我尝试更改主机,但没有任何效果。
下面是我尝试在生产环境中部署的代码片段。



const express = require("express");
const session = require("express-session");
const redis = require("redis");
const client = redis.createClient(process.env.REDIS_URL);
const redisStore = require("connect-redis")(session);
const cors = require("cors");
const cookieParser = require("cookie-parser");
const authRouter = require("../Auth/authRouter");
const plansRouter = require("../Plans/plansRouter");

// IM NOT SURE IF I NEED THIS CODE BELOW COMMENTED OUT

// const redis = require("redis");
// if (process.env.NODE_ENV == "production") {
// client = redis.createClient(process.env.REDIS_URL);
// } else {
// client = redis.createClient();
// }

const server = express();
server.use(
cors({
credentials: true,
origin: "https://client.nicholaspatterson36.now.sh"
})
);
server.use(express.json());
server.use(cookieParser("this is my secret"));
server.use(
session({
secret: "this is my secret",
name: "sequelize",
unset: "destroy",
rolling: true,
credentials: "include",
cookie: { secure: false },
store: new redisStore({
host: "localhost",
port: 6379,
client: client,
ttl: 86400
}),
saveUninitialized: false,
resave: false
})
);
server.use("/auth", authRouter);
server.use("/plans", plansRouter);
console.log(client);

client.on("connect", () => {
console.log(`\n ** Redis client connected ** \n`);
});
client.on("error", function(err) {
console.log("Something went wrong " + err);
});

module.exports = server;

最佳答案

您应该将其应用于heroku设置-> config vars->添加REDIS_URL。
并且,最好在heroku附加组件中找到并应用redis云。

您可以参考以下链接:https://github.com/RedisLabs/rediscloud-node-sample/blob/master/web.js

关于javascript - 如何在Node JS中使用Redis session 设置Heroku Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58149191/

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