如何用Node.js结合Redis,轻松高效处理各种数据任务?
- 内容介绍
- 文章标签
- 相关推荐
在开始这篇文章之前请确保安装好 Redis 和 Node.js 还有 Node.js 的 Redis —— node_redis
说到环境准备,快速摆脱安装烦恼
1️⃣ **Node.js**
bash
sudo apt-get update && sudo apt-get install nodejs npm
说到检查版本。node -v && npm -v
2️⃣ **Redis**
bash
sudo apt-get install redis-server
sudo systemctl start redis-server && sudo systemctl status redis-server
常见痛点这方面,* Windows 使用者需要使用 WSL 或 Docker。* macOS 安装失败时建议先更新 Homebrew。* 安装后忘记开启服务导致连接错误 “ECONNREFUSED”。
一、Node.js 与 Redis 基础连接
js const redis = require;const client = redis.createClient({ 再看host,'127.0.0.1'。port: 6379,password: undefined // 如有密码请填入 });client.on => console.log);client.on),如果你遇到 “Connection refused” 或 “Auntication error”,请检查:
-
/etc/redis/redis.conf是否开启了 requirepass。 -
/etc/hosts中是否存在冲突。
在开始这篇文章之前请确保安装好 Redis 和 Node.js 还有 Node.js 的 Redis —— node_redis
说到环境准备,快速摆脱安装烦恼
1️⃣ **Node.js**
bash
sudo apt-get update && sudo apt-get install nodejs npm
说到检查版本。node -v && npm -v
2️⃣ **Redis**
bash
sudo apt-get install redis-server
sudo systemctl start redis-server && sudo systemctl status redis-server
常见痛点这方面,* Windows 使用者需要使用 WSL 或 Docker。* macOS 安装失败时建议先更新 Homebrew。* 安装后忘记开启服务导致连接错误 “ECONNREFUSED”。
一、Node.js 与 Redis 基础连接
js const redis = require;const client = redis.createClient({ 再看host,'127.0.0.1'。port: 6379,password: undefined // 如有密码请填入 });client.on => console.log);client.on),如果你遇到 “Connection refused” 或 “Auntication error”,请检查:
-
/etc/redis/redis.conf是否开启了 requirepass。 -
/etc/hosts中是否存在冲突。

