0
votes

So I am trying to make a simple proxy (I think that's the right word) and I've come up with some code that works fine locally. I can call 'firebase serve --only functions' and the function works fine and I get expected results. Now when I deploy this same code, and try calling it it just times out. I have no idea why, so I was hoping I could get some help.

Here's the code:

//Variables
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const request = require('request');

//App
const app = express();
app.use(cors({ origin: true }));

//Endpoints
app.get('/**', function(req, res) {
    request('https://example.com' + req.url, function(err, proxyRes, body) {

        //Err
        if (err) {
            res.send('Error: ' + err.code);
            return
        }

        //Res
        res.status(200).send(body);
    });
});

//Functions
exports.proxy = functions.https.onRequest(app);
1

1 Answers

1
votes

HTTP functions will time out if they don’t send a response. This means your request() is probably failing, and it’s probably failing because, on the free Spark payment plan, you can’t make outgoing requests to services that Google doesn’t fully control.

Your function should send a response in all conditions in order to avoid a timeout. This means you should be checking for errors all the time.