Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve 'Error: connect ECONNREFUSED 127.0.0.1:9200' using ElasticSearch with Firebase?

I know that this question has been asked several times, but the solutions didn't apply to my situation. I have started ElasticSearch, Kibana using these 2 commands:

bin/elasticsearch
bin/kibana

And the server is running well, when using browser to go to http://localhost:9200, I get the following result:

{
    "name" : "vg7RnLh",
    "cluster_name" : "elasticsearch",
    "cluster_uuid" : "gqDf0H67TxGZGYigAlu2mQ",
    "version" : {
        "number" : "6.1.2",
        "build_hash" : "5b1fea5",
        "build_date" : "2018-01-10T02:35:59.208Z",
        "build_snapshot" : false,
        "lucene_version" : "7.1.0",
        "minimum_wire_compatibility_version" : "5.6.0",
        "minimum_index_compatibility_version" : "5.0.0"
    },
    "tagline" : "You Know, for Search"
}

And when using kibana, everything works fine. Here is the firebase cloud function I wrote, if there's anything wrong, I think it's here:

exports.downloadUserInfo = functions.https.onRequest((req, res) => {
    const { uid } = req.query;

    const searchConfig = {
        uri: 'http://localhost:9200/users/user/' + uid,
        method: 'GET'
    };

    request(searchConfig)
        .then((result) => {
           console.log(result);
           return res.status(200).send(result);
       })
           .catch((error) => {
               console.log(error);
               return res.status(400).send(error);
           });
});

Where request is a module install by yarn add request request-promise. Then, when using Postman to test my API, I get this error:

enter image description here

How do I resolve this?

Update

I am also running flashlight using command node app.js. When inserting a new user to the database, the new user is automatically inserted to the ElasticSearch server too (localhost), however, when I try to read, it throws request error

Second Update

This is the API that creates a user:

exports.register = functions.https.onRequest((req, res) => {
    const { firstName, lastName } = req.body;

    admin.database().ref('users').push({ firstName, lastName })
        .then(() => res.status(201).send())
        .catch(() => res.status(400).send());
});

When a new user is created by this API, this user is automatically inserted to my localhost ElasticSearch server, because I am using flashlight plugin. I don't understand why when I try to read from the same server, there's request error.

like image 226
K.Wu Avatar asked Jan 23 '18 00:01

K.Wu


2 Answers

Your code is trying to access the local host:

uri: 'http://localhost:9200/users/user/' + uid,

This won't work: you can't just access "random" ports on the container where your Cloud Functions run.

My guess is that you have ElasticSearch running on your local machine, and that's what localhost is meant to refer to. But to run the code in the Cloud Functions environment, you'll need to have ElasticSearch running on a system that is publicly accessible, and specify the IP of that machine in the uri.

like image 132
Frank van Puffelen Avatar answered Sep 16 '22 14:09

Frank van Puffelen


Cloud Functions run on a server that Google controls. They don't run on your machine (except when you're using local emulation for testing).

Your function is accessing a URL at "localhost":

const searchConfig = {
    uri: 'http://localhost:9200/users/user/' + uid,
    method: 'GET'
};

request(searchConfig).then(...)

In nearly every context, localhost means IP address 127.0.0.1 - the same machine where the code is running. When your code is running in Cloud Functions, that means localhost is the Google server instance running your code. This is most certainly not what you want, and unfortunately, you won't be able to access services running on your personal machine, either.

like image 38
Doug Stevenson Avatar answered Sep 17 '22 14:09

Doug Stevenson