title: Cloud Functions for Firebase and Express
body: <p>The Firebase docs for the <code>functions.https</code> namespace shows that the function accepts an <code>express.Request</code> object and an <code>express.Response</code> object. Nowhere does it mention that you can pass an express server object to <code>functions.https.onRequest</code>. However, I've found that people have been doing this with no clear indication from commenters that this shouldn't be done (except one person in the <code>functions-samples</code> repo issue #101 thread)</p>
  
  <p>see:</p>
  
  <ul>
  <li><code>firebase-functions</code>
  <a href="https://github.com/firebase/firebase-functions/issues/27" rel="noreferrer">https://github.com/firebase/firebase-functions/issues/27</a></li>
  <li><code>functions-samples</code> for middleware
  <a href="https://github.com/firebase/functions-samples/blob/master/authorized-https-endpoint/functions/index.js" rel="noreferrer">https://github.com/firebase/functions-samples/blob/master/authorized-https-endpoint/functions/index.js</a></li>
  </ul>
  
  <p>My questions are then:</p>
  
  <ol>
  <li>How do Cloud Functions for Firebase or GCP Cloud Functions handle the lifetime of objects initialized outside of the function definition?</li>
  <li>How does the above affect the lifetime of the function? Does it run until timeout or function similarly to AWS Lambda?</li>
  </ol>
  
  <p>Clarification for 1 &amp; 2: In Lambda any resources outside of the exported function is used on all subsequent invocations of the same Lambda instance while that function instance is "warm". This means the response time of the function is not negatively affected by any complex initialization code you may have beforehand as it is done once per "warm" instance. In this example, it wouldn't then need to initialize an ExpressJS server each invocation, just once while the function is "warm". I'm curious if Cloud Functions do the same?</p>
  
  <p>Also in Lambda, the existence of the ExpressJS server does not extend the execution time of the function (when it returns it's done), I'm also curious how Cloud Functions is implemented here. Does it simply do the same as Lambda, or (because it may handle existing objects differently) does it do something else?</p>
  
  <ol start="3">
  <li>The <code>functions.https.onRequest</code> documentation doesn't specify you can pass an ExpressJS server object into it, so how is this working? Are there then two endpoints? Can someone explain what is happening here?</li>
  </ol>
  
  <p>Clarification for 3: I've been seeing people do the following:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// './functions/index.js'
  
  var functions = require("firebase-functions");
  const express = require("express");
  
  // setup ExpressJS Server
  const expressRouter = new express.Router();
  expressRouter.get("*", (req, res) =&gt; {
    res.send(`Hello from Express in Cloud Functions for Firebase`);
  });
  
  // Cloud Function
  exports.express = functions.https.onRequest(expressRouter);</code></pre>
  </div>
  </div>
  </p>
  
  <p>And wish to know how this works given the Cloud Functions API only specifies accepting <code>functions.https.onRequest(request, response)</code> params modelled after the ExpressJS API.</p>
  
  <blockquote>
    <p>These parameters are based on the Express Request and Response objects - firebase.google.com/docs/functions/http-events</p>
  </blockquote>
  
  <p>Since all questions pertain to the single snippet of code and this one use case I thought it would be better answered together.</p>
  
  <p>Thanks in advance :)</p>