node.jsgoogle-cloud-functionsfunctions-framework

Sending 200 success using node js Google Cloud Function


I'm trying to send a 200 - Success back using the 'res' parameter of a google cloud function. I kept getting the error 'TypeError: res.setStatusCode is not a function.' even though this should be a part of the object. So I did the below to make sure.

const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('app', (req, res) => {
    
    console.log(typeof res); // "Function"
    console.log(Object.keys(res)); // []
    console.log(res.statusCode); // undefined
    console.log(res.headers); // undefined
    console.log(res.body); // undefined
    
    res.setStatusCode(400);
    res.send('Success');
    
});

It appears res is actually a function (not an object) and statusCode is not defined. Is this correct or Am I missing something here? How do I change this to send a 200 while using @google-cloud/functions-framework? Thank you.


Solution

  • As @cmgchess suggested, this was achieved by using functions.http. I'm adding an example code block for anyone who may get the same problem.

    const functions = require('@google-cloud/functions-framework');
    
    functions.http('app', (req, res) => {
       //Do your work
       res.status(200).send('Success');
    });