首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Redis连接到127.0.0.1:6379失败-连接ECONNREFUSED 127.0.0.1:6379 nodejs

Redis连接到127.0.0.1:6379失败-连接ECONNREFUSED 127.0.0.1:6379 nodejs
EN

Stack Overflow用户
提问于 2019-04-24 05:33:07
回答 1查看 2.2K关注 0票数 1
代码语言:javascript
复制
LT027296-Mac:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mongo               latest              394204d45d87        3 weeks ago         410MB
redis               latest              a55fbf438dfd        4 weeks ago         95MB
nginx               latest              2bcb04bdb83f        4 weeks ago         109MB
bitnami/mysql       latest              c5c056b8435c        3 months ago        287MB
LT027296-Mac:~$ docker run --name some-redis -d redis
15e126e26ea452b2b8c2933c549a15d74bb49aece1fe8b5e4b746e67bced6c20
LTB0207296-Mac:~ b0207296$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
15e126e26ea4        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp            some-redis
LT027296-Mac:~$ 

嗨,我正在从链接https://medium.com/tech-tajawal/introduction-to-caching-redis-node-js-e477eb969eab下面学习redis教程。

我执行以下步骤

  1. 从码头下载redis图像
  2. 然后运行容器(如上面所示)
  3. 然后我运行我的代码
代码语言:javascript
复制
const express = require('express')
const fetch = require("node-fetch");
const redis = require('redis')

// create express application instance
const app = express()

// create and connect redis client to local instance.
const client = redis.createClient()

// echo redis errors to the console
client.on('error', (err) => {
    console.log("Error " + err)
});

// get photos list
app.get('/photos', (req, res) => {
  // key to store results in Redis store
  const photosRedisKey = 'user:photos';

  // Try fetching the result from Redis first in case we have it cached
  return client.get(photosRedisKey, (err, photos) => {

    // If that key exists in Redis store
    if (photos) {

      return res.json({ source: 'cache', data: JSON.parse(photos) })

    } else { // Key does not exist in Redis store

      // Fetch directly from remote api
      fetch('https://jsonplaceholder.typicode.com/photos')
        .then(response => response.json())
        .then(photos => {

        // Save the  API response in Redis store,  data expire time in 3600 seconds, it means one hour
        client.setex(photosRedisKey, 3600, JSON.stringify(photos))

        // Send JSON response to client
        return res.json({ source: 'api', data: photos })

      })
        .catch(error => {
        // log error message
        console.log(error)
        // send error to the client
        return res.json(error.toString())
      })
    }
  });
});

// start express server at 3000 port
app.listen(3000, () => {
  console.log('Server listening on port: ', 3000)
});

我收到了这个错误

Redis连接到127.0.0.1:6379失败-连接ECONNREFUSED 127.0.0.1:6379

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-24 06:23:45

在运行docker映像时,您还必须发布端口。

代码语言:javascript
复制
docker run --name some-redis -d redis -p 6379:6379

这将把docker的端口映射到host端口。

您还可以选择传递主机:

代码语言:javascript
复制
-p 127.0.0.1:8001:8001

阅读有关发布here的更多信息

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55822997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档