How to use Firebase Cloud Functions - javascript

I am a developer with very little knowledge about Firebase Cloud Functions and programming in general. I followed the 'Get started' guide on the 'Firebase Cloud Functions' and have got to this point:
https://imgur.com/a/gjgEE
Now, where should I do all the Node.js coding work? Also, what is the index.js folder and where can I find it?

Running firebase init functions will have created the project files and index.js file + other files and directories as specified in the guide. You should run the init functions command in a directory that you make specifically for the project, not your home directory of \Users\Arjun Ram. I would recommend you do the following:
md FirebaseProject
cd FirebaseProject
firebase init functions
dir
This will create a folder for your project, cd into the folder, initialize the firebase project in the folder, and then list the contents of the folder to show you the files the command created. When you do dir, you should see a firebase.json and a functions/ directory which contains the index.js. You can then continue to proceed with the Getting Started guide.

Related

How to share code between client and cloud functions [duplicate]

I have a Node server and multiple controllers that perform DB operations and helpers (For e-mail, for example) within that directory.
I'd like to use source from that directory within my functions. Assuming the following directory structure:
src/
server/
/app/controllers/email_helper.js
fns/
send-confirm/
What's the best way to use email_helper within the send-confirm function?
I've tried:
Symbolically linking the 'server' directory
Adding a local repo to send-confirm/package.json
Neither of the above work.
In principle, your Cloud Functions can use any other Node.js module, the same way any standard Node.js server would. However, since Cloud Functions needs to build your module in the cloud, it needs to be able to locate those dependency modules from the cloud. This is where the issue lies.
Cloud Functions can load modules from any one of these places:
Any public npm repository.
Any web-visible URL.
Anywhere in the functions/ directory that firebase init generates for you, and which gets uploaded on firebase deploy.
In your case, from the perspective of functions/package.json, the ../server/ directory doesn't fall under any of those categories, and so Cloud Functions can't use your module. Unfortunately, firebase deploy doesn't follow symlinks, which is why that solution doesn't work.
I see two possible immediate fixes:
Move your server/ directory to be under functions/. I realize this isn't the prettiest directory layout, but it's the easiest fix while hacking. In functions/package.json you can then have a local dependency on ./server.
Expose your code behind a URL somewhere. For example, you could package up a .tar and put that on Google Drive, or on Firebase Cloud Storage. Alternatively, you can use a public git repository.
In the future, I'd love it if firebase deploy followed symlinks. I've filed a feature request for that in Firebase's internal bug tracker.

Which files are sent to the Firebase cloud functions environment when you deploy the functions?

Let's say I have the following structure inside my functions folder for my Firebase app.
functions
> node_modules // INSTALLED NODE MODULES
> distApp // REACT APP FILES TRANSPILED WITH BABEL
App.js
index.html
> distFunctions // FUNCTION FILES TRANSPILED WITH BABEL
function1.js // SOME OF THEM USE FILES FROM 'distApp' FOLDER
function2.js
> src // FUNCTION FILES WRITTEN IN ES6+
function1.js
function2.js
indexES6.js // CLOUD FUNCTIONS index.js WRITTEN IN ES6+
index.js // CLOUD FUNCTIONS index.js TRANSPILED WITH BABEL
package.json
QUESTION
I would like to understand what happend when I deploy my index.js file.
What files are going to be available inside my Node.js environment? Do all files inside my functions folder (and subfolders) are going to be sent to my Node.js environment?
What if none of my functions use (require) a file named someFile.xxx. But that file is sitting there inside of one of my functions subfolders. Is it going to be sent to the Cloud Functions environment?
The node_modules folder is ignored during the deployment and packages are installed inside the Node.js environment in the Cloud. Am I right?
NOTE: This functions folder lives inside my Firebase project root folder, where I have a firebase.json file and everything else needed for deployment.
PS: I know it's not best practice to ask more than one question here on SO, but they're all related to the main question: "Exactly which files and folders are deployed to the cloud functions environment when you run firebase deploy --only functions "?
Everything from your functions folder gets deployed, except node_modules. It doesn't matter what your index.js contains.
Cloud Functions will reconstitute your node_modules folder on the backend by running npm install. So, the contents of your package.json matters a lot.
In the end, Cloud Functions builds a docker image and puts all those files and modules from your functions folder into it, and it will all be available when your function executes.

Maven Profile concept in a NodeJS Project without MVN

Hello stakOverFlowers :D
I have a simple NodeJS WebApp that use Lerna to manage the project. So i have a package directory that contains n different projects each ones using different tasks runner tools.
I always use Maven Build Profile in java environment but for this NodeJS project maven will not be used.
So the question is...
Is there a way to reproduce the Maven Build Profile concept without using MVN?
In a nutshell i need to use a build profile in nodejs, without using MVN, to customize build for different environments such as Production v/s Development environments.
There's a way to do that?
thanks to all
you can do it by storing your configurations in a JSON file as key value pairs in the same way as you do in properties file in Java.
Then by someway or other invoke properties from the environment specific configuration file such as production.json or stage.json or qa.json.
One of the easy ways to do this is using this module named config
Using this you can pass NODE_ENV=production(or dev or qa whatever) and access relevant configurations. This will help you achieve environment profiling.
You can also store configurations in a JS file but I personally prefer a JSON file for storing configurations.
But if you're wondering for dependencies management that is done by package.json file which is somewhat similar to your pom.xml file.
For more details about it you might want to read this official documentation for it.
My solution, following the TGW advice, works!!
Just install the config module, and create a directory that containing .json files.
$ npm install config
$ mkdir config
$ vi config/default.json
Than if u are on a windows machine, choose your NODE_ENV using NODE_ENV=production and than execute your web app.
In your .js file put informations like yours dbConnection host and password.... and to retrieve it use:
var config = require('config');
var dbConfig = config.get('JsonItem.dbConfig');
..more details on https://github.com/lorenwest/node-config

MSBuild: publish generated files and ignoring previous generated files that are no longer available

So following this thread In Visual Studio, how can I set the Build Action for an entire folder?
the solution works for me. I have a webpack configuration that creates hashed files in a Build folder that get published. The problem that I have now is that the files that are no longer generated are still available on live.
Here's the scenario:
On every build the script removes everything from the ./Build folder
First time index.js and main.js are generated and are published using wildcard configuration in .csproj file
Second time ./Build folder is again cleaned and only index.js is generated however main.js is still available on live.
I guess it is a problem as beside .js and .css media files are bundled as well, images and fonts are loaded and hashed and in current state it will become quite crowded with a bunch of versions of the same files.
Any idea how to solve this?
Ok so the problem in my case was with the release configuration.
Using TFS as VCS. There I have Visual Studio Build with MSBuild x86 set in advanced configuration. MSBuild configuration has the wildcard to include all files and folders from ./Build folder, which it did.
Now to the problem - for deployments/release I again use a TFS configuration, there I have a task Azure App Service Deploy. This takes the packaged from build configuration and deploy it on server. In the Additional Deployment Options group there's the Remove Additional Files at Destination. Without it files that don't have any matches in the deployed package are not deleted.
Exemple 1 without Remove Additional Files at Destination option:
Deploy location has files: a.js, b.js, c.js
On TFS Build a new package is created with updated a.js, c.js and a new file z.js. Package does no longer contains the file b.js;
The package is deployed
After deploy, deploy location has files: a.js(updated), b.js, c.js(updated), z.js(new)
Exemple 2 with Remove Additional Files at Destination option:
Deploy location has files: a.js, b.js, c.js
On TFS Build a new package is created with updated a.js, c.js and a new file z.js. Package does no longer contains the file b.js;
The package is deployed
After deploy, deploy location has files: a.js(updated), c.js(updated), z.js(new), b.js file is removed from deploy location.

How do I deploy to heroku from git when my API key is in a .gitignore file?

I have my API key set to a variable in an apikey.js file, and I reference the variable in another javascript file where the API key is supposed to be.
I added apikey.js to .gitignore so that people wouldn't see it when I pushed it to my (public) github account.
However, when I try to deploy, the app doesn't work because of the .gitignore.
How can I continue to push up files omitting the API key to my repo on git while deploying on heroku?
use https://github.com/ddollar/heroku-config, and you can keep your secrets in a file called .env and ignore that in .gitignore
Install it with heroku plugins:install git://github.com/ddollar/heroku-config.git
You can run heroku config:pull --overwrite --interactive to generate an initial .env file that includes your service secrets, etc, and heroku config:push to save it, remotely.
I am assuming that you are using node, since your config file is javascript. To get the values in your .env file in node use process.env. For example to connect to your mongolab in mongoose:
mongoose.connect(process.env.MONGOLAB_URI);
There is more about all this here: https://devcenter.heroku.com/articles/config-vars#local-setup

Categories