Search code examples
node.jsfirebasegoogle-cloud-functionsfirebase-cli

Using Express in firebase calls function but creates a timeout


I'm a bit at loss here.

I have the following node.js setup:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cookieParser = require('cookie-parser');
var https = require('https');

admin.initializeApp();

let app = express();

app.use(cookieParser);


// Set/Get Cookie

app.get('/setuser', (req, res) => {
    console.log('**************SET USER');
    console.log('**************req:');
    console.log(req);

    console.log('**************res:');
    console.log(res);
    res.send('setting user');
});

app.get('/getuser', (req, res) => {

    console.log('**************GET USER');
    console.log('**************req:');
    console.log(req);

    console.log('**************res:');
    console.log(res);
    res.send('getting user');
    //res.send(req.cookies);
});

exports.main = functions.https.onRequest(app);

In the firebase.json file I have:

"hosting": {
    "site": "sso-login",
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/main/**",
        "function": "main"
      },
      {
        "source": "**",
        "destination": "index.html"
      }
    ]
  }

I'm running the whole app in locally in the firebase simulator.

Now, when I call

https://localhost:5100/main/getuser Or https://localhost:5100/main/getuser/

The simulator's console says Beginning execution of "main" - and then nothing happens.

If I remove the rewrites from the firebase.json, I get a simple 404-error.

How do I make my setuser/getuser-functions work?


Solution

  • you must send a response in firebase HTTP functions. the simulator stuck probably because of comment on the response lines. you can read about in the docs https://firebase.google.com/docs/functions/http-events