I have a firebase cloud function which will be invoked on HTTP request which is working fine.
Now, I want to read data from a JSON file for some business logic. Below are the 2 ways I was trying to read the JSON file:
Option #1) Saved the JSON file inside 'public' directory in my nodejs project and deployed. Got a Hosting URL which I am using like below. But its throwing an error saying 'Error: getaddrinfo ENOTFOUND...'
Option #2) Uploaded the JSON file to firebase cloud storage. Didnt find any example to try this out. Ended up with the below code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const http = require('http');
const url = require('url');
// Option #2 required variables
var storage = require('@google-cloud/storage');
const gcs = storage({projectId: "<Project ID>"});
const bucket = gcs.bucket("<bucket-name>");
const file = bucket.file("<filename.json>")
// HTTP Trigger
exports.functionName = functions.https.onRequest((req, res) => {
var request = require('request');
var paramValue = req.body.queryParam;
console.log(paramValue);
// Option #1 - Using hosted URL
var hostingURL = "https://xxxxxxxx.firebaseapp.com/filename.json";
console.log(hostingURL);
request({
url:hostingURL,
method: 'POST',
json:{ key: 'value' } },function(error, response, data) {
});
// Option #2 - Ended up here. Want to read from cloud storage bucket.
console.log(file);
});
Can some one help me?