<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Omar Houmz</title>
    <description>The latest articles on DEV Community by Omar Houmz (@omarhoumz).</description>
    <link>https://dev.to/omarhoumz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F113089%2Fa4c47c69-a499-4b4d-912a-cd609d69f0df.jpg</url>
      <title>DEV Community: Omar Houmz</title>
      <link>https://dev.to/omarhoumz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omarhoumz"/>
    <language>en</language>
    <item>
      <title>Host almost anything with firebase hosting</title>
      <dc:creator>Omar Houmz</dc:creator>
      <pubDate>Wed, 07 Apr 2021 23:00:00 +0000</pubDate>
      <link>https://dev.to/omarhoumz/host-almost-anything-with-firebase-hosting-2cp3</link>
      <guid>https://dev.to/omarhoumz/host-almost-anything-with-firebase-hosting-2cp3</guid>
      <description>&lt;p&gt;&lt;em&gt;This was originally posted here: &lt;a href="https://www.omarhoumz.com/blog/firebase-hosting/"&gt;https://www.omarhoumz.com/blog/firebase-hosting/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Almost anytime you want to share a new trick or showcase something, you will need a way to host your web pages or app. Firebase Hosting can help you.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Firebase Hosting?
&lt;/h3&gt;

&lt;p&gt;As the Firebase Hosting &lt;a href="https://firebase.google.com/docs/hosting"&gt;docs&lt;/a&gt; mention:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Firebase Hosting provides fast and secure hosting for your web app, static and dynamic content, and microservices.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, whether you have some static HTML/CSS, a reactjs, vuejs, or svelte app, you can use Firebase Hosting to deploy your code to a global CDN.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get started?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Install the Firebase cli, here I’m using &lt;em&gt;npm&lt;/em&gt; (&lt;a href="https://firebase.google.com/docs/cli"&gt;check the docs&lt;/a&gt; for other methods):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1npm install -g firebase-tools

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Login to your account using the Firebase cli:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1firebase login

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should redirect you to a web page to login into your account (create one if you don’t have it already). Once logged in, you should see a “Login successful” message in your terminal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Init a firebase project&lt;/p&gt;

&lt;p&gt;This step involves linking your project (the code you want to deploy) to a firebase project. You can use an existing project or create a new one.&lt;/p&gt;

&lt;p&gt;To do this, let’s run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1firebase init hosting

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CLI will prompt us to “Use an existing project” or to “Create a new project”, among other options. For me, I’ll create a new project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1+ .firebaserc

2+ 404.html

3+ firebase.json

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After following the steps, we should have some new files. &lt;code&gt;.firebaserc&lt;/code&gt; contains the project id. The &lt;code&gt;firebase.json&lt;/code&gt; file has the hosting config; public folder location and other things. You will also notice a &lt;code&gt;404.html&lt;/code&gt; page that is created by default by Firebase (if you don’t already have one in your project).&lt;/p&gt;

&lt;p&gt;These first three steps are identical regardless of your project type; Static HTML, reactjs, nextjs, vuejs, or svelte app, etc …&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Deploying static HTML&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the case of a static HTML project, I created a project with only one &lt;code&gt;index.html&lt;/code&gt; file here: &lt;a href="https://github.com/omarhoumz/wr-firebase-hosting/blob/main/static-html/index.html"&gt;https://github.com/omarhoumz/wr-firebase-hosting/blob/main/static-html/index.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can clone it to follow along.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 4: Deploy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now, we can deploy our website (our index.html in this case) to Firebase Hosting using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1firebase deploy --only hosting

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the deployment is successful, you should see the URL to your newly deployed website in your terminal. It should be in this format: &lt;code&gt;&amp;lt;project-id&amp;gt;.web.app&lt;/code&gt;. You can get your &lt;code&gt;project-id&lt;/code&gt; in the &lt;code&gt;.firebaserc&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1{

2"projects": {

3"default": "&amp;lt;project-id&amp;gt;"

4}

5}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For me, this is the URL I ended up with &lt;a href="http://wr-firebase-hosting.web.app"&gt;wr-firebase-hosting.web.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Check the whole static HTML project here: &lt;a href="https://github.com/omarhoumz/wr-firebase-hosting/tree/main/static-html"&gt;github.com/omarhoumz/wr-firebase-hosting/tree/main/static-html&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create-React-App app
&lt;/h2&gt;

&lt;p&gt;I created a CRA sample app here: &lt;a href="https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-cra"&gt;https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-cra&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: build our project:
&lt;/h3&gt;

&lt;p&gt;We can build our CRA app using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1npm run build

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is will create a &lt;code&gt;build&lt;/code&gt; folder. And this is the folder we should deploy to Firebase. But how to tell Firebase about it? Good question.&lt;/p&gt;

&lt;p&gt;Remember the &lt;code&gt;firebase.json&lt;/code&gt; file? We need to change the &lt;code&gt;"public"&lt;/code&gt; key in that config file from whatever it is now to &lt;code&gt;"build"&lt;/code&gt;, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1- "public": "public",

2+ "public": "build",

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Deploy
&lt;/h3&gt;

&lt;p&gt;Now we are all set to deploy our CRA app to firebase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1firebase deploy --only hosting

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And again, if the deployment is successful, you should see the URL to your newly deployed website in your terminal. It should be in this format &lt;code&gt;&amp;lt;project-id&amp;gt;.web.app&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;For me, this is the URL I ended up with &lt;a href="http://wr-firebase-hosting-cra.web.app"&gt;wr-firebase-hosting-cra.web.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You’ll the whole project here: &lt;a href="https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-cra"&gt;https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-cra&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  NextJs app
&lt;/h2&gt;

&lt;p&gt;NextJs handles hybrid static &amp;amp; server rendering. And many more features. Firebase Hosting allows only to host static files. However, we can go around it by using &lt;a href="https://firebase.google.com/docs/functions"&gt;Firebase Cloud Function&lt;/a&gt; and do some &lt;a href="https://firebase.google.com/docs/hosting/full-config#rewrites"&gt;fancy Hosting rewrites&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  SSG content step 4: build our project
&lt;/h3&gt;

&lt;p&gt;We’ll need to build and “export” our next app, using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1# yarn

2yarn build &amp;amp;&amp;amp; yarn export

3

4# npm

5npm run build &amp;amp;&amp;amp; npm run export

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an &lt;code&gt;out&lt;/code&gt; folder. And this is the folder we need to deploy. We need to change the &lt;code&gt;"public"&lt;/code&gt; key in the &lt;code&gt;firebase.json&lt;/code&gt; file from whatever it is now to &lt;code&gt;"out"&lt;/code&gt;, like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1- "public": "public",

2+ "public": "out",

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SSG content step 5: Deploy
&lt;/h3&gt;

&lt;p&gt;After a successful deployment, you should see the URL to your newly deployed website in your terminal. It should be in this format &lt;code&gt;&amp;lt;project-id&amp;gt;.web.app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For me, this is the URL I ended up with &lt;a href="https://wr-firebase-hosting-next.web.app/"&gt;wr-firebase-hosting-next.web.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You’ll the whole project here: &lt;a href="https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-next"&gt;https://github.com/omarhoumz/wr-firebase-hosting/tree/main/firebase-is-fire-next&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The steps to host anything in Firebase hosting is pretty easy once you know the basics. Fundamentally, you identify the folder where your static files are. You tell Firebase about it (using the “public” key). And then use the command to deploy your code.&lt;/p&gt;

&lt;p&gt;From here, you can setup your CI/CD pipeline to automate this process.&lt;/p&gt;

&lt;p&gt;Firebase Hosting has more to it than what I showed here. And even more products by Firebase. You can check them all at &lt;a href="https://firebase.google.com/"&gt;firebase.google.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any question don’t hesitate to email me or shoot me a DM &lt;a class="mentioned-user" href="https://dev.to/omarhoumz"&gt;@omarhoumz&lt;/a&gt;
 on twitter.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create your first Firebase Cloud function</title>
      <dc:creator>Omar Houmz</dc:creator>
      <pubDate>Sat, 30 May 2020 15:51:36 +0000</pubDate>
      <link>https://dev.to/omarhoumz/how-to-create-your-first-firebase-cloud-function-4igb</link>
      <guid>https://dev.to/omarhoumz/how-to-create-your-first-firebase-cloud-function-4igb</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted in &lt;a href="https://omarhoumz.com/blog/firebase-cloud-functions/"&gt;https://omarhoumz.com/blog/firebase-cloud-functions/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this post we will cover one small, but very much hyped, part of serverless; &lt;strong&gt;cloud functions&lt;/strong&gt;. One of the solutions available is firebase's &lt;a href="https://firebase.google.com/docs/functions" rel="nofollow noopener noreferrer"&gt;"Firebase Cloud Functions"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are going to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create and set up a Firebase project&lt;/li&gt;
&lt;li&gt;Setup a Firebase cloud function&lt;/li&gt;
&lt;li&gt;Deploy our first function&lt;/li&gt;
&lt;li&gt;Setup a local dev environment&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Requirements
&lt;/h4&gt;

&lt;p&gt;I assume that you have node and npm installed on your machine.&lt;/p&gt;

&lt;h4&gt;
  
  
  Starter project
&lt;/h4&gt;

&lt;p&gt;Before we start, let us set up a basic project to work with. I created some files and boilerplate code in &lt;a href="https://github.com/omarhoumz/wr-firebase-clound-function/tree/master/001-create-a-firebase-function-start" rel="nofollow noopener noreferrer"&gt;this repo&lt;/a&gt; in the &lt;code&gt;001-create-a-firebase-function-start&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Check the &lt;a href="https://github.com/omarhoumz/wr-firebase-clound-function/blob/master/001-create-a-firebase-function-start/README.md" rel="nofollow noopener noreferrer"&gt;readme file&lt;/a&gt; on how to get started with the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create and set up a Firebase project
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Create a Firebase project
&lt;/h4&gt;

&lt;p&gt;To create a firebase project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to the &lt;a href="https://console.firebase.google.com/" rel="nofollow noopener noreferrer"&gt;Firebase Console&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Log in or create your account&lt;/li&gt;
&lt;li&gt;Once on the home page, click the big "Add project" button&lt;/li&gt;
&lt;li&gt;Enter a name for your project&lt;/li&gt;
&lt;li&gt;Click "Continue" and follow the steps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And voila, we have a Firebase project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setup our firebase project locally
&lt;/h4&gt;

&lt;p&gt;First, we have to install the Firebase CLI tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; firebase-cli
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, we login to Firebase from the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;firebase login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will open a browser tab for us to authenticate to Firebase console. Once authenticated successfully, we should have our terminal prompt back.&lt;/p&gt;

&lt;p&gt;Next is to initialize a Firebase project locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;firebase init functions
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The command will guide us through the setup. It will ask about 3 things: the Firebase project (which we already created), Cloud Functions language (I chose JavaScript, you can also choose TypeScript), and whether or not to use ESLint (which I replied with a "yes, please" to).&lt;/p&gt;

&lt;p&gt;If all goes well, we should see these files created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-
&lt;/span&gt;&lt;span class="gi"&gt;+ .firebaserc
+ .gitignore
+ firebase.json
+ functions/.eslintrc.json
+ functions/.gitignore
+ functions/index.js
+ functions/package-lock.json
+ functions/package.json
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In addition to some other files you see, these are the important ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.firebaserc&lt;/code&gt;: stores your project aliases.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;firebase.json&lt;/code&gt;: lists your project configuration.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;functions/package.json&lt;/code&gt; node dependencies for our Firebase cloud function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The file that we should take a look at now is &lt;code&gt;functions/index.js&lt;/code&gt;. This is where we write our code for the cloud function.&lt;/p&gt;

&lt;p&gt;Let us use this "helloWorld" example. Notice that I disabled the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="nofollow noopener noreferrer"&gt;&lt;code&gt;CORS&lt;/code&gt; header&lt;/a&gt; since we are in development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;functions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;firebase-functions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;functions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onRequest&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// disable CORS header&lt;/span&gt;
  &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Access-Control-Allow-Origin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;And, hello from serverless!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Deploy our first cloud function
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Test the Firebase function locally
&lt;/h4&gt;

&lt;p&gt;Before we deploy, we want to test the function locally. To do so, let us &lt;code&gt;cd&lt;/code&gt; into the &lt;code&gt;Functions&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;functions
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And start a local emulator (this is provided by the firebase-cli tools):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this runs, the function will be watched for changes, and the local URL should be logged in the console. Look for something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;✔  functions[helloWorld]: http &lt;span class="k"&gt;function &lt;/span&gt;initialized &lt;span class="o"&gt;(&lt;/span&gt;http://localhost:5001/YOUR_PROJECT_ID/us-central1/helloWorld&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the link above, &lt;code&gt;YOUR_PROJECT_ID&lt;/code&gt; and &lt;code&gt;us-central1&lt;/code&gt; are the project id and the data center region for your Firebase project respectively. And &lt;code&gt;helloWorld&lt;/code&gt; is the function name.&lt;/p&gt;

&lt;p&gt;If we visit the URL and all goes well, we see this message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"And, hello from serverless land!"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Deploy the Firebase cloud function
&lt;/h4&gt;

&lt;p&gt;Now, let us deploy our function. While still being in the functions directory, execute this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result of the command should have a public URL to trigger our function. It looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://${DATA_CENTER_REGION}-${YOUR_PROJECT_ID}.cloudfunctions.net/${FUNCTION_NAME}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Trigger the cloud function
&lt;/h4&gt;

&lt;p&gt;Next, let us use the function in our project. To do that, we add a &lt;code&gt;div&lt;/code&gt; in the &lt;code&gt;index.html&lt;/code&gt; file to show a message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;/*&lt;/span&gt; index.html */
  &amp;lt;h1&amp;gt;Hello Firebase Functions&amp;lt;/h1&amp;gt;
&lt;span class="gi"&gt;+ &amp;lt;h2 id="function-response"&amp;gt;&amp;lt;/h2&amp;gt;
+
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And in the &lt;code&gt;index.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// get the heading element&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;messageDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#function-response&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// a function that updates the heading&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;messageDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// make sure to replace these info with yours&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;YOUR_PROJECT_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_PROJECT_ID&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DATA_CENTER_REGION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DATA_CENTER_REGION&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FUNCTION_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FUNCTION_NAME&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// the function trigger endpoint&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`https://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;DATA_CENTER_REGION&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;YOUR_PROJECT_ID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.cloudfunctions.net/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;FUNCTION_NAME&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;

&lt;span class="c1"&gt;// trigger the function and get the result back&lt;/span&gt;
&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="c1"&gt;// when we have the result, we update the heading with the message&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;updateMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After we made these changes, let us check the result. Run the dev server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If everything went well, we should see in our browser a message like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Firebase Functions
And, hello from serverless land!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  4. A better local development setup
&lt;/h3&gt;

&lt;p&gt;When we are in development, we don't want to constantly deploy our function to see the result. So, we will leverage the local emulator provided by the Firebase CLI tools to have a better local development setup.&lt;/p&gt;

&lt;p&gt;We know how to start the function emulator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;functions &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm run serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We know how to start our dev server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;parcel ./index.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we need a way to start both of them with one command. Here is the easiest way I could find:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;functions &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm run serve&lt;span class="o"&gt;)&lt;/span&gt; &amp;amp; parcel ./index.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to keep this setup, we're gonna edit the scripts in our &lt;code&gt;package.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;"scripts":&lt;/span&gt; {
&lt;span class="gd"&gt;-  "start": "parcel ./index.html"
&lt;/span&gt;&lt;span class="gi"&gt;+  "start:frontend": "parcel ./index.html",
+  "start": "(cd functions &amp;amp;&amp;amp; npm run serve) &amp;amp; npm run start:frontend"
&lt;/span&gt;&lt;span class="err"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we can use one command to start them both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For the &lt;code&gt;url&lt;/code&gt; variable, we should modify it to accomodate for the environment (development vs production):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`https://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;DATA_CENTER_REGION&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;YOUR_PROJECT_ID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.cloudfunctions.net/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;FUNCTION_NAME&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`http://localhost:5001/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;YOUR_PROJECT_ID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;DATA_CENTER_REGION&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;FUNCTION_NAME&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now that we use the local url of the function, we can remove the &lt;code&gt;CORS&lt;/code&gt; header in the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- // disable CORS header
- response.setHeader('Access-Control-Allow-Origin', '*')
&lt;/span&gt;&lt;span class="gi"&gt;+ // disable CORS header for development
+ if (process.env.NODE_ENV !== 'production') {
+   response.setHeader('Access-Control-Allow-Origin', '*')
+ }
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then deploy the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;functions &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm run deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Until this point, we created a Firebase project using the Firebase console. In our local machine, we initialized our project a Firebase function and deployed it. We also configured a better setup for local development.&lt;/p&gt;

&lt;p&gt;There is still much we can do here. We will explore other things in the next posts. But, if you are in a hurry, you can check the &lt;a href="https://firebase.google.com/docs/functions" rel="nofollow noopener noreferrer"&gt;the Firebase Functions docs&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>cloudfunctions</category>
      <category>serverless</category>
    </item>
  </channel>
</rss>
