0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm on the Blaze plan, not the spark plan.
You still need to be checking for errors and sending a response back to the client in all circumstances.
How would I do that, I updated the code but it's still showing the same error. Updated the original post
It's entirely possible also that the request is taking longer than the default timeout of 60 seconds for a Cloud Function. The only way you can know that is to increase the timeout in the cloud console, or impose a timeout on your own request to stop before 60s.
Also you might want to consider logging things so you can see what parts of your code are actually being accessed. Without logs, you are really just guessing what's happening.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.