I implemented the following function in my Firebase with Cloud Functions backend. Becasue, I want to be able to have my Twitch API token in an environment variable, because the frontend is pure HTML and JavaScript with no Node whatsoever.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as cors from 'cors';
admin.initializeApp();
const db = admin.firestore();
const corsHandler = cors({ origin: true });
import fetch from 'node-fetch';
export const twitchStatus = functions.https.onRequest(async (req, res) => {
corsHandler(req, res, async () => {
// const origin = req.get('origin') || req.get('referer');
// if (origin !== allowedOrigin) {
// res.status(403).json({ error: "Access forbidden, Invalid origin" });
// return;
// }
try {
const { channelName } = req.body;
const url = `https://api.twitch.tv/helix/streams?user_login=${channelName}`;
const clientId = process.env.TWITCH_CLIENT_ID as string;
const accessToken = process.env.TWITCH_ACCESS_TOKEN as string;
const response = await fetch(url, {
method: 'GET',
headers: {
'Client-ID': clientId,
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
res.status(201).json({ message: "Twitch status created successfully", data: data});
} catch (error) {
res.status(500).json({ error: "An error occurred while creating the twitch status." });
}
});
});
When trying to deploy, I get the following error:
i functions: updating Node.js 18 (2nd Gen) function twitchStatus(us-central1)...
Could not create or update Cloud Run service twitchstatus, Container Healthcheck failed. Revision 'twitchstatus-00001-rej' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.
Logs URL: ---
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
Functions deploy had errors with the following functions:
twitchStatus(us-central1)
i functions: cleaning up build files...
! functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at ---
I have four more similar functions that deploy as expected without errors.
What is going wrong here?