googlearchive / flashlight

A pluggable integration with ElasticSearch to provide advanced content searches in Firebase.
http://firebase.github.io/flashlight/
756 stars 144 forks source link

Getting 'connect ECONNREFUSED 127.0.0.1:9200' error when executing cloud function #180

Closed newlifepop closed 6 years ago

newlifepop commented 6 years ago

I have a users table in my database, and this is the Cloud Function 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());
});

In my config.js file in Flashflight folder, I am monitoring this table:

exports.FB_URL = process.env.FB_URL || '<Here I have my database url, it's correct>';
exports.FB_REQ = process.env.FB_REQ || 'search/request';
exports.FB_RES = process.env.FB_RES || 'search/response';
exports.FB_SERVICEACCOUNT = process.env.FB_ACC || 'service-account.json';

if (process.env.BONSAI_URL) {
    processBonsaiUrl(exports, process.env.BONSAI_URL);
}
else {
    exports.ES_HOST = process.env.ES_HOST || 'localhost';
    exports.ES_PORT = process.env.ES_PORT || '9200';
    exports.ES_USER = process.env.ES_USER || null;
    exports.ES_PASS = process.env.ES_PASS || null;
}
exports.paths = [
    {
        path: 'users',
        index: 'users',
        type: 'user'
    }
];
exports.ES_OPTS = {
    requestTimeout: 60000, maxSockets: 100, log: 'error'
};

exports.CLEANUP_INTERVAL =
    process.env.NODE_ENV === 'production' ?
        3600 * 1000 /* once an hour */ :
        60 * 1000 /* once a minute */;

function processBonsaiUrl(exports, url) {
    var matches = url.match(/^https?:\/\/([^:]+):([^@]+)@([^/]+)\/?$/);
    exports.ES_HOST = matches[3];
    exports.ES_PORT = 80;
    exports.ES_USER = matches[1];
    exports.ES_PASS = matches[2];
    console.log('Configured using BONSAI_URL environment variable', url, exports);
}

And then after I start elasticsearch server (localhost:9200) and flashlight, everything works fine, when I create a new user using the API above, I also get this from flashlight terminal:

screen shot 2018-01-22 at 23 27 31

which proves that everything is working fine, the user is inserted into the database, and flashlight also inserted the user into ES server. However, when I have another cloud function:

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) => res.status(200).send(result))
        .catch((error) => res.status(400).send(error));
});

Where request is installed by yarn add request request-promise, when I use this API, I get error like this:

screen shot 2018-01-22 at 23 30 35

How do I resolve this issue?