I am writing a cloud function to start or stop a server for a game panel I'm using. Everything works except when the request completes it never triggers the "data", "end", or "closed" events which cause Firebase to hang. Has anyone else encountered this before? I've googled for "Node http hanging after receiving 204 response" but didn't find anything relevant.
import * as functions from "firebase-functions";
import * as http from "https";
import * as qs from "querystring";
export const commandServer = functions.https.onRequest((request, response) => {
if (request.body.command !== "start" && request.body.command !== "stop") {
response.status(400).send("Error, missing information.");
}
console.log(request.body.origin);
const apiKey = "<apiKey>";
const panelServerId = "<panelId>";
const data = qs.stringify({
signal: request.body.command === "start" ? "start" : "stop",
});
const options = {
protocol: "https:",
hostname: "<server url>",
port: 443,
path: `/api/client/servers/${panelServerId}/power?`,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": data.length,
Authorization: `Bearer ${apiKey}`,
},
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", () => {
response.status(200).send(`Successfuly sent ${data} command`);
});
});
req.on("error", (error) => {
console.log("error: ", error);
response.status(500).send("Oops, something went wrong!");
});
req.write(data);
req.end();
});