index.js

  1. const redis = require("redis");
  2. // 创建客户端
  3. const redisClient = redis.createClient(6379, "127.0.0.1");
  4. redisClient.on("error", (err) => {
  5. console.error(err);
  6. });
  7. // 测试
  8. redisClient.set("myname", "wcd2", redis.print);
  9. redisClient.get("myname", (err, val) => {
  10. if (err) {
  11. console.error(err);
  12. return;
  13. }
  14. console.log("val", val);
  15. // 退出
  16. redisClient.quit();
  17. });

package.json

  1. {
  2. "name": "redis-test",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1"
  8. },
  9. "keywords": [],
  10. "author": "",
  11. "license": "ISC",
  12. "dependencies": {
  13. "redis": "^2.8.0"
  14. }
  15. }