Learn GOOGLE-CLOUD-FUNCTIONS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple Google Cloud Function (Node.js)
# gcf/demo/index.js
exports.helloWorld = (req, res) => {
res.status(200).send('Hello, Google Cloud!');
};
A simple Google Cloud Function responding with 'Hello, Google Cloud!' to HTTP requests.
2
Google Cloud Function with Query Parameters
# gcf/demo/query.js
exports.queryFunction = (req, res) => {
const name = req.query.name || 'Guest';
res.status(200).send(`Hello, ${name}!`);
};
Reads query parameters and responds with a personalized message.
3
Google Cloud Function with JSON Response
# gcf/demo/json.js
exports.jsonFunction = (req, res) => {
res.status(200).json({ message: 'Hello, JSON!' });
};
Returns a JSON object in the response body.
4
Google Cloud Function POST Handler
# gcf/demo/post.js
exports.postFunction = (req, res) => {
const data = req.body;
res.status(200).send(`Received: ${data.input}`);
};
Handles POST requests and parses JSON body.
5
Google Cloud Function with Environment Variables
# gcf/demo/env.js
exports.envFunction = (req, res) => {
const secret = process.env.MY_SECRET || 'No Secret';
res.status(200).send(`Secret is: ${secret}`);
};
Uses environment variables in the function.
6
Google Cloud Function Redirect
# gcf/demo/redirect.js
exports.redirectFunction = (req, res) => {
res.redirect(302, 'https://cloud.google.com/');
};
Responds with a redirect to another URL.
7
Google Cloud Function Error Handling
# gcf/demo/error.js
exports.errorFunction = (req, res) => {
try {
throw new Error('Something went wrong');
} catch(err) {
res.status(500).send(err.message);
}
};
Demonstrates returning an error response.
8
Google Cloud Function Delayed Response
# gcf/demo/delay.js
exports.delayFunction = async (req, res) => {
await new Promise(resolve => setTimeout(resolve, 1000));
res.status(200).send('Delayed Hello!');
};
Returns a response after a simulated delay.
9
Google Cloud Function Fetch External API
# gcf/demo/fetch.js
const fetch = require('node-fetch');
exports.fetchFunction = async (req, res) => {
const response = await fetch('https://api.github.com');
const data = await response.json();
res.status(200).json(data);
};
Fetches data from an external API and returns it.
10
Google Cloud Function with Custom Headers
# gcf/demo/headers.js
exports.headersFunction = (req, res) => {
res.set('X-Custom-Header', 'GCF');
res.status(200).send('Hello with headers');
};
Returns custom HTTP headers in the response.