<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Archer on Medium]]></title>
        <description><![CDATA[Stories by David Archer on Medium]]></description>
        <link>https://medium.com/@davestevearch01?source=rss-358791f8cc18------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IKwXFSBCkPJsuQY7gX__gg.jpeg</url>
            <title>Stories by David Archer on Medium</title>
            <link>https://medium.com/@davestevearch01?source=rss-358791f8cc18------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:44:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@davestevearch01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Firebase Function API with Express.js and Testing it on a Firebase Emulator]]></title>
            <link>https://medium.com/@davestevearch01/building-a-firebase-function-api-with-express-js-and-testing-it-on-a-firebase-emulator-ce4c0638e165?source=rss-358791f8cc18------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce4c0638e165</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[firebase-cloud-functions]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[node-js-tutorial]]></category>
            <dc:creator><![CDATA[David Archer]]></dc:creator>
            <pubDate>Tue, 28 Feb 2023 11:19:06 GMT</pubDate>
            <atom:updated>2023-02-28T11:19:06.976Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tczgP83CVQ4ShCEWGDPXcw.jpeg" /></figure><p>Firebase is a popular cloud-based platform that provides a range of services, including authentication, hosting, and a real-time database. One of the key features of Firebase is its serverless computing platform, which allows you to create backend services without the need to manage servers or infrastructure.</p><p>This tutorial will show you how to create a Firebase Function API using Express.js and test it on a Firebase emulator. Specifically, we will create an endpoint that posts data to Firestore and test it using Jest.js and Supertest.js.</p><h3>Prerequisites</h3><p>Before you get started, you should have the following:</p><ul><li>A Firebase account (visit <a href="https://firebase.google.com/">Firebase (google.com)</a> to create an account)</li><li>Node.js installed on your computer</li><li>An IDE or code editor of your choice</li></ul><h3>Setting Up</h3><p>To begin, let’s create a new Firebase project and initialize a new Node.js project.</p><ol><li>Create a new Firebase project by going to the Firebase Console and clicking on the “Add Project” button.</li><li>Install the Firebase CLI by running the following command:</li></ol><pre>npm install -g firebase-tools</pre><p>3. Initialize a new Firebase project by running the following command:</p><pre>firebase init</pre><p>This will prompt you to select the Firebase features you want to use. For this tutorial, we will select “Functions” and “Emulators”.</p><p>4. Once the initialization process is complete, navigate to the “functions” directory and run the following command to install the required dependencies:</p><pre>npm install express cors body-parser firebase-functions firebase-admin</pre><p>5. Now let’s create a new Express.js app in the index.js file:</p><pre>const functions = require(&quot;firebase-functions&quot;);<br>const express = require(&quot;express&quot;);<br>const cors = require(&quot;cors&quot;);<br>const cors = require(&quot;body-parser&quot;);<br>const {getFirestore} = require(&quot;firebase-admin/firestore&quot;);<br><br>const app = express();<br>const firestore = getFirestore();<br><br>app.use(bodyParser.json());<br>app.use(cors());<br>app.use(express.json());<br><br>// Define endpoint<br>app.post(&quot;/getStartedData&quot;, async (req, res) =&gt; {<br>  try {<br>    const data = req.body;<br>    const docRef = await firestore.collection(&quot;getStartedData&quot;).add(data);<br>    res.status(201).json({ id: docRef.id });<br>  } catch (error) {<br>    console.error(error);<br>    res.status(500).json({ error: &quot;Unable to add data.&quot; });<br>  }<br>});<br><br>// Export the app<br>api.exports = functions.https.onRequest(app);</pre><p>In this code, we are importing the required dependencies, creating a new Express.js app, and defining an endpoint that posts data to Firestore.</p><p>We are also using the cors middleware to allow cross-origin requests, Body-parser body parsing middleware is responsible for parsing the incoming request bodies and the firebase-admin/firestore package to interact with Firestore.</p><h3>Testing on a Firebase Emulator</h3><p>Now that we have created our Express.js app, let’s test it on a Firebase emulator.</p><ol><li>First, let’s install the required testing dependencies:</li></ol><pre>npm install jest supertest firebase-functions-test --save-dev</pre><p>2. Next, create a new test file in the functions directory called index.test.js Write a test case for the /getStartedData endpoint:</p><pre>const test = require(&quot;firebase-functions-test&quot;)( {<br>    projectId: &quot;my-test-project&quot;,<br>    databaseURL: &quot;http://localhost:8080&quot;,<br>  },&quot;./path/to/serviceAccountKey.json&quot;);<br>const express = require(&quot;express&quot;);<br>const cors = require(&quot;cors&quot;);<br>const supertest = require(&quot;supertest&quot;);<br>const { getFirestore } = require(&quot;firebase-admin/firestore&quot;);<br>const bodyParser = require(&quot;body-parser&quot;);<br>const {api}= require(&quot;./index&quot;);<br>const firestore = getFirestore();<br><br>describe(&#39;GET /getStartedData&#39;, () =&gt; {<br>  let app;<br>  let request;<br><br>  beforeAll(() =&gt; {<br>firestore.settings({<br>      host: (process.env.FIRESTORE_EMULATOR_HOST = &quot;localhost:8080&quot;),<br>      ssl: false,<br>    });<br><br>    app = express();<br>    app.use(cors());<br>    app.use(&quot;/&quot;, api);<br>    app.use(bodyParser.json());<br>    app.use(bodyParser.urlencoded({ extended: true }));<br>    request = supertest(app);<br>  });<br><br>  it(&#39;should create a new document in Firestore&#39;, async () =&gt; {<br>  <br><br>    const res = await request.post(&quot;/getStarted&quot;).send({<br>      title: &#39;Test post&#39;,<br>      content: &#39;This is a test post&#39;,<br>    });<br>    expect(res.status).toBe(200);<br>  });<br>});<br><br></pre><p>Run the test suite by executing the following command:</p><pre>npm test</pre><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce4c0638e165" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>