简体   繁体   中英

Connect to Elasticsearch in a Fastify app with username and password via @fastify/elasticsearch package

I'm a new Fastify user. I created a fastify@4.0.0 app via fastify-cli . I want to connect to Elasticsearch via username and password, but in the @fastify/elasticsearch documentation there is no mention of it. As the documentation said:

const fastify = require('fastify')()

fastify.register(require('@fastify/elasticsearch'), { node: 'http://xxxx:9200' })

fastify.get('/user', async function (req, reply) {
  const { body } = await this.elastic.search({
    index: 'tweets',
    body: {
      query: { match: { text: req.query.q }}
    }
  })

  return body.hits.hits
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
});

I don't know how to set a username and password to connect to my Elasticsearch instance. Is it true?

fastify.register(require('@fastify/elasticsearch'), { node: 'http://8.8.8.8:9200', username: 'xxxx', password: 'xxxx' });

I finally found it. Just follow this pattern <protocol>://<username>:<password>@<host>:<port> .

fastify.register(require('@fastify/elasticsearch'), { node: 'http://xxxx:xxxx@8.8.8.8:9200' });    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM