Nodejs:appfog 怎么用node连接自带的redis
Nodejs:appfog 怎么用node连接自带的redis
文档没看懂
{"redis-2.2":[
{
"name":"redis-example",
"label":"redis-2.2",
"plan":"free",
"tags":["redis","redis-2.2","key-value","nosql"],
"credentials":{
"hostname":"10.7.66.164",
"host":"10.7.66.164",
"port":5004,
"password":"191dc43f-69a3-4d31-ac5e-4b66155b2e8e",
"name":"9e3f3a9c-82ba-4c2e-bc83-b498e5447251"
}
}
]}
是不是要这样写
var options = {
host: '10.7.66.164',
hostname:'10.7.66.164',
port: 5004,
prefix: 'chs-sess',
pass:这里怎么写?
还有有这里貌似没有写name的选项
}
app.use(express.cookieParser());
app.use(express.session({ store: new RedisStore(options), secret: 'zclcat' }))
还有prefix有什么用
Node.js: 如何在 AppFog 上使用 Node 连接自带的 Redis
背景信息
AppFog 是一个基于云的应用程序托管平台。在其平台上,你可以轻松地为你的应用添加各种服务,例如 Redis 数据库。本文将介绍如何在 AppFog 环境中使用 Node.js 连接到自带的 Redis 服务。
获取 Redis 配置信息
首先,你需要从 AppFog 平台获取 Redis 服务的配置信息。根据你提供的 JSON 数据,Redis 服务的配置信息如下:
{
"redis-2.2": [
{
"name": "redis-example",
"label": "redis-2.2",
"plan": "free",
"tags": ["redis", "redis-2.2", "key-value", "nosql"],
"credentials": {
"hostname": "10.7.66.164",
"host": "10.7.66.164",
"port": 5004,
"password": "191dc43f-69a3-4d31-ac5e-4b66155b2e8e",
"name": "9e3f3a9c-82ba-4c2e-bc83-b498e5447251"
}
}
]
}
使用 Node.js 连接 Redis
为了在 Node.js 中连接到 Redis,我们需要安装 redis
模块。可以使用 npm 安装该模块:
npm install redis
接下来,我们编写代码来连接到 Redis 服务:
const redis = require('redis');
// 从 AppFog 提供的配置信息中获取 Redis 的连接信息
const redisConfig = {
host: '10.7.66.164',
port: 5004,
password: '191dc43f-69a3-4d31-ac5e-4b66155b2e8e'
};
// 创建 Redis 客户端
const client = redis.createClient(redisConfig.port, redisConfig.host, {
auth_pass: redisConfig.password
});
// 监听错误事件
client.on('error', (err) => {
console.error('Redis error:', err);
});
// 测试连接
client.ping((err, res) => {
if (err) throw err;
console.log('Redis is connected and responded with:', res);
});
关于 prefix
prefix
参数通常用于在存储键时添加前缀,以避免不同应用程序之间的键冲突。例如,如果你的应用需要存储用户会话数据,你可以在存储键时添加前缀,如 app-session:
。这样,其他应用程序的键不会与你的键冲突。
使用 Express 和 RedisStore
如果你想在 Express 应用中使用 Redis 作为会话存储,可以使用 express-session
和 connect-redis
模块:
npm install express express-session connect-redis
然后,在你的 Express 应用中设置会话存储:
const express = require('express');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const app = express();
// 配置 Redis 存储
const redisStoreOptions = {
host: '10.7.66.164',
port: 5004,
password: '191dc43f-69a3-4d31-ac5e-4b66155b2e8e',
prefix: 'app-session:'
};
app.use(session({
store: new RedisStore(redisStoreOptions),
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));
app.get('/', (req, res) => {
req.session.views = (req.session.views || 0) + 1;
res.send(`You have viewed this page ${req.session.views} times.`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
通过以上步骤,你就可以成功地在 AppFog 上使用 Node.js 连接到自带的 Redis 服务,并利用 Redis 存储会话数据。
https://docs.appfog.com/add-ons/rediscloud#rediscloud-node
var redis = require('redis');
var url = require('url');
var redisURL = url.parse(process.env.REDISCLOUD_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);
谢谢,不过这个是在add-ons里,我选的是在service,貌似也没有输入用户名密码的,直接能用
https://docs.appfog.com/services/redis
process.env.VCAP_SERVICES
这个取到的就是 那个 json对象 var env = JSON.parse(process.env.VCAP_SERVICES); var db = env[‘redis-2.2’][0][‘credentials’]; store: new RedisStore(db); 这样就ok了,要学会看文档!
错了 var db = env[‘redis-2.2’][‘credentials’]; 没有那个[0]我是在我mysql上改的改错了
针对AppFog自带的Redis服务,你可以使用redis
模块来连接Redis。下面是具体的步骤和示例代码:
-
首先需要安装
redis
模块,可以通过npm来安装:npm install redis --save
-
使用配置信息来创建Redis客户端实例。根据你提供的JSON格式配置信息,可以将配置信息提取出来,并用其值创建Redis客户端实例。
-
示例代码如下:
const redis = require('redis');
// 提取配置信息
const credentials = {
hostname: '10.7.66.164',
port: 5004,
password: '191dc43f-69a3-4d31-ac5e-4b66155b2e8e'
};
// 创建Redis客户端实例
const client = redis.createClient(credentials.port, credentials.hostname, {
auth_pass: credentials.password
});
client.on('error', (err) => {
console.error(`Redis error: ${err}`);
});
client.on('connect', () => {
console.log('Connected to Redis');
});
// 简单的测试:设置一个键值对
client.set('testKey', 'testValue', (err, reply) => {
if (err) throw err;
console.log(reply); // 如果成功,则打印"OK"
});
// 获取刚刚设置的键值对
client.get('testKey', (err, reply) => {
if (err) throw err;
console.log(reply); // 应该打印"testValue"
});
关于prefix
参数,它通常用于express-session
的Redis存储适配器中,用来为存储在Redis中的所有session key添加一个前缀,以避免与其他应用或同个应用下的其他session key冲突。但是在这个例子中,我们直接使用redis
模块与Redis交互,所以不需要设置prefix
参数。