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?
There is no reason to Firebase not work with EJS or even DigitalOcean. If you are integrating Firebase in front-end your problem is not back-end related.
Firebase Hosting provides fast and secure hosting for your web app, static and dynamic content, and microservices.
Yes, it's possible. You can place the .json file in the same folder, where your index.js is. Then you can do the following: const config = require ('./config.json'); console.log (config.foo); Thanks @Thomas, your solution works fine. Do you also know how to read it from firebase cloud storage bucket (as I mentioned in option #2) ?
The JSON files can also be read from URLs. However, several methods are available to read JSON data from a URL in Javascript like jquery, loadJSON, etc. But we will use the loadJSON () function to read a JSON file from the URL.
The fetch API is the preferable method to use when we want to read a JSON file either from an external server or local file into our JavaScript file. Another method we can use aside from making an HTTP request is the import statement. This method has a few complications, but we will address them all.
One standard method we can use to read a JSON file (either a local file or one uploaded to a server) is with the Fetch API. It uses the same syntax for both. The only difference would be the URL. For example, suppose we have a local file within our project's folder named data.json that contains the following JSON data:
You can place the .json file in the same folder, where your index.js is. Then you can do the following:
const config = require('./config.json');
console.log(config.foo);
Given following config.json file:
{
"foo" : "bar"
}
If your file is in Firebase Could Storage you can use this approach:
const admin = require('firebase-admin');
admin.storage().bucket().file("yourDirForFile/yourFile.json")
.download(function (err, contents) {
if (!err) {
var jsObject = JSON.parse(contents.toString('utf8'))
}
});
variable jsObject you can use as you wish. It is in memory for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With