hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Cannot create property 'path' on string 'localhost' when specifying host and port on cache property #167

Closed geuis closed 7 years ago

geuis commented 8 years ago

Originally from https://github.com/hapijs/hapijs.com/issues/407

I encounter a stacktrace when host and port aren't used on an options property.

In the Client subsection of http://hapijs.com/tutorials/caching, this configuration code example is given:

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server({
    cache: [
        {
            name: 'mongoCache',
            engine: require('catbox-mongodb'),
            host: '127.0.0.1',
            partition: 'cache'
        },
        {
            name: 'redisCache',
            engine: require('catbox-redis'),
            host: '127.0.0.1',
            partition: 'cache'
        }
    ]
});

server.connection({
    port: 8000
});

I am only able to get this to work with an options property:

const server = new Hapi.Server({
  debug: {
    request: ['error']
  },
  cache: {
    name: 'redisCache',
    engine: CatBox,
    options: {
      host: process.env.redisHost,
      port: process.env.redisPort
    }
  }
});

This is the stacktrace I get when I try using host and port directly on cache: {}

server-1 TypeError: Cannot create property 'path' on string 'localhost'
server-1     at createClient (/usr/local/apps/projecto/node_modules/redis/lib/createClient.js:62:26)
server-1     at Object.exports.createClient (/usr/local/apps/projecto/node_modules/redis/index.js:1081:41)
server-1     at internals.Connection.start (/usr/local/apps/projecto/node_modules/catbox-redis/lib/index.js:46:24)
server-1     at internals.Client.start (/usr/local/apps/projecto/node_modules/hapi/node_modules/catbox/lib/client.js:41:21)
server-1     at each (/usr/local/apps/projecto/node_modules/hapi/lib/server.js:247:36)
server-1     at Object.exports.parallel (/usr/local/apps/projecto/node_modules/hapi/node_modules/items/lib/index.js:70:13)
server-1     at internals.Server.initialize (/usr/local/apps/projecto/node_modules/hapi/lib/server.js:251:11)
server-1     at internals.Server.start (/usr/local/apps/projecto/node_modules/hapi/lib/server.js:182:10)
server-1     at Object.<anonymous> (/usr/local/apps/projecto/dist/webpack:/app/app.js:119:8)
server-1     at __webpack_require__ (/usr/local/apps/projecto/dist/webpack:/webpack/bootstrap c4781f2bce8764d4cada:19:1)

This is my app.js setup:

const server = new Hapi.Server({
  debug: {
    request: ['error']
  },
  cache: {
    name: 'redisCache',
    engine: CatBox,
// host and port must be commented out to avoid the error
    host: process.env.redisHost,
    port: process.env.redisPort
// No errors when "options" is uncommented
//    options: {
//      host: process.env.redisHost,
//      port: process.env.redisPort
//    }
  }
});

server.connection({
  host: process.env.serverHost,
  port: process.env.serverPort
});

// redis cache
server.app.cache = server.cache({
  cache: 'redisCache',
  segment: 'seg',
  expiresIn: 604800000 // 1 week
});

And my package.json dependencies

package.json

"dependencies": {
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-polyfill": "^6.9.0",
    "babel-runtime": "^6.9.0",
    "bcrypt": "^0.8.7",
    "catbox-redis": "^2.0.2",
    "dotenv": "^2.0.0",
    "hapi": "^13.4.1",
    "hapi-auth-cookie": "^6.1.1",
    "joi": "^8.4.2",
    "mysql": "^2.11.1",
    "node-uuid": "^1.4.7",
    "nunjucks": "^2.4.2",
    "source-map-support": "^0.4.0",
    "vision": "^4.1.0"
  }

One thought is that the babel es6 transpiling is doing something that catbox doesn't like.

cnadeau commented 7 years ago

I ran into the same issue using redis.createClient and it was because the args are: createClient(port, host)

https://github.com/NodeRedis/node_redis/blob/master/lib/createClient.js#L6

inverting them caused this error

hueniverse commented 7 years ago

Is this a catbox-redis bug then?

kanongil commented 7 years ago

This is a catbox-redis issue. From the looks of it this can happen if you pass an empty or non-digit string as the port.

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.