Skip to content
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

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

Closed
MichaelAnckaert opened this issue Nov 15, 2018 · 4 comments
Closed

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

MichaelAnckaert opened this issue Nov 15, 2018 · 4 comments
Labels

Comments

@MichaelAnckaert
Copy link

I'm trying to get elasticsearch-js working in a browser application using the http-aws-es 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? I did some debugging and it seems that the check in ConnectionAbstract is failing:
`
function ConnectionAbstract(host, config) {
config = config || {};
EventEmitter.call(this);

this.log = config.log || new Log();
this.pingTimeout = config.pingTimeout || 3000;

if (!host) {
throw new TypeError('Missing host');
} else if (host instanceof Host) {
this.host = host;
} else {
throw new TypeError('Invalid host');
}
utils.makeBoundMethods(this);
}
`

@wheresrhys
Copy link

I'm having a similar problem. From reading AWS docs https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-handling-errors.html#aes-troubleshooting-host-header a host header should be sent, but elasticsearch-js isn't transparent about whether it sets a host header, or what mechanism there is to set one. The code has references to params.headers, but this is not documented https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html

@wheresrhys
Copy link

More digging - there is a host class that can be used when setting up a client. This allows explicit setting of headers: https://github.com/elastic/elasticsearch-js/blob/9c98262b871f860afccc04f8025c35c45aea2281/docs/host.asciidoc

Please can maintainers make this easier to find. No links at all from the readme

@swbradshaw
Copy link

I had the same problem. In my Angular client I was also using elasticsearch-browser and http-aws-es. I was able to get it working by removing elasticsearch-browser and using elasticsearch. Those 2 libraries are conflicting because your Host class in one is not the same as the Host class in the other.

@stale
Copy link

stale bot commented Mar 15, 2019

We understand that this might be important for you, but this issue has been automatically marked as stale because it has not had recent activity either from our end or yours.
It will be closed if no further activity occurs, please write a comment if you would like to keep this going.

Note: in the past months we have built a new client, that has just landed in master. If you want to open an issue or a pr for the legacy client, you should do that in https://github.com/elastic/elasticsearch-js-legacy

@stale stale bot added the stale label Mar 15, 2019
@stale stale bot closed this as completed Mar 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants