Skip to content

"Invalid Host" error when running in the browser #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MichaelAnckaert opened this issue Nov 15, 2018 · 2 comments
Open

"Invalid Host" error when running in the browser #59

MichaelAnckaert opened this issue Nov 15, 2018 · 2 comments

Comments

@MichaelAnckaert
Copy link

I'm trying to get http-aws-es working in a browser application using the elasticsearch-js library. My code is currently looking like this but I'm getting a "TypeError: invalid host" in the browser console.

`
let AWS = require('aws-sdk')

  let options = {
    hosts: ['https://my-es-host.es.amazonaws.com'],
    connectionClass: require('http-aws-es'),
    awsConfig: new AWS.Config({
      credentials: new AWS.Credentials('AKA...', 'secret'),
      region: 'eu-central-1'
    }),
    httpOptions: {}
  }

  let es = require('elasticsearch-browser').Client(options)

  es.ping({
    requestTimeout: 30000
  }, function (error) {
    if (error) {
      console.error('elasticsearch cluster is down!')
    } else {
      console.log('All is well')
    }
  })`

Did anyone get a similar setup working in the browser and what am I doing wrong?

@printscreen
Copy link

Did you ever find the solution to this problem? I am having the same problem.

@printscreen
Copy link

printscreen commented Jan 26, 2019

So this may not apply to other people, but I did find the issue for me.
I wrote https://www.npmjs.com/package/elasticulize-cli

I was trying to work with an AWS ECS Container to automatically run a migration. Up till this point I have only ever used it locally with docker-compose.

I ran into your issue where I would get the following error.

(node:776) UnhandledPromiseRejectionWarning: TypeError: Invalid host
    at HttpAmazonESConnector.ConnectionAbstract (/usr/local/lib/node_modules/elasticsearch/src/lib/connection.js:27:11)
    at new HttpConnector (/usr/local/lib/node_modules/elasticsearch/src/lib/connectors/http.js:32:22)
    at new HttpAmazonESConnector (/usr/local/lib/node_modules/http-aws-es/connector.js:20:5)
    at ConnectionPool.setHosts (/usr/local/lib/node_modules/elasticulize-cli/node_modules/elasticsearch/src/lib/connection_pool.js:320:20)
    at Transport.setHosts (/usr/local/lib/node_modules/elasticulize-cli/node_modules/elasticsearch/src/lib/transport.js:451:23)
    at new Transport (/usr/local/lib/node_modules/elasticulize-cli/node_modules/elasticsearch/src/lib/transport.js:68:10)
    at new EsApiClient (/usr/local/lib/node_modules/elasticulize-cli/node_modules/elasticsearch/src/lib/client.js:58:22)
    at new Client (/usr/local/lib/node_modules/elasticulize-cli/node_modules/elasticsearch/src/lib/client.js:101:10)
    at Promise (/usr/local/lib/node_modules/elasticulize-cli/lib/commands.js:40:16)
    at <anonymous>

If you look at line 27, the check of host instanceOf Host was failing. After some debugging, i could clearly prove the host was an instanceOf Host.

My fix was just toying with it and I found the fix. Up till this point, http-aws-es was installed on the container globally. That was the issue. Once i installed it locally to /usr/local/lib/node_modules/elasticulize/node_modules it worked.

I dont know why, but there is something weird going on when http-aws-es is not installed to the local node_modules folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants