Learn how to initialize Firebase's cloud functions in your app. You will also test out your first cloud function with the Firebase Emulator Suite.
Instructor: We'll use the Firebase emulator to develop our Cloud Functions locally. If you have Firebase Cloud Functions already initialized in your project, you can go to the index.ds file inside of your functions directory. It will look a little bit like this -- it will have the import functions, and it will have an example function commented out.
If you haven't initialized it, you can open the terminal and type, firebase init functions. It will prompt the Firebase CLI to help you initialize your project. In this case, we're going to select the language to write the functions, we're going to use TypeScript. It's going to say, "Do you want to use ESLint to catch probably bugs?" we're going to say, "Yes."
Then this is because I already had them initialized. It's asking me if I want to replace the files I already have here. I'm going to choose, "No," because I'm already using them.
Then, once the initialization is complete, we can go back into our index file. In here, we are going to uncomment the function that they have as an example.
This is an HTTP function. It's going to return as string text that says "Hello" from Firebase. I'm going to change this to say "Hello" from Firebase emulator and to run this.
The first thing that we need to do is to navigate into the functions folder. Right now, let me make this a little bigger. Right now, I'm in the root folder of my project.
Depending if you are using Angular, React view, it's going to look a little bit different. In here, you should have the functions folder. I'm going to navigate into the functions folder, clear again the console.
In here, I can see that the Cloud functions have a package.json file. You're going to be able to see the scripts that you can use. I'm going to run the npm run build to build the Cloud functions.
After they're built, it's important to remember to navigate out of the folder, and then we can run the Firebase emulator again. Firebase emulator start.
Once the emulator is up and running, we can go here in the console and we can look for the functions simulator. We can click on view logs, and it's going to take us here, and it says, "HTTP function initialized."
Let's make that a little bit bigger. It gives you the URL you can use as an endpoint. It's localhost:5001, the name of the project -- in this case for me it's, however, examples -- the region that the functions are deployed, and the name of the function.
We can test this directly from the console. For example, I can do curl and paste the URL there. When I hit Enter, you see that it says, "Hello from the Firebase emulator." Inside of the console, you can see the logs where it says, "Beginning execution of the function," "Finished execution of the function," saying that everything went well.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!