This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {google} = require('googleapis'); | |
/** | |
* Returns a list of Google APIs and their descriptions. | |
* @param {Express.Request} req The API request. | |
* @param {Express.Response} res The API response. | |
*/ | |
exports.listGoogleAPIs = async (req, res) => { | |
// Call the API | |
const apis = await google.discovery('v1').apis.list(); | |
// Build the response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use the official Node.js 12 image. | |
# https://hub.docker.com/_/node | |
FROM node:12 | |
# Create and change to the app directory. | |
WORKDIR /usr/src/app | |
# Copy application dependency manifests to the container image. | |
# A wildcard is used to ensure both package.json AND package-lock.json are copied. | |
# Copying this separately prevents re-running npm install on every code change. | |
COPY package*.json ./ | |
# Install production dependencies. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ffnode12", | |
"version": "1.0.0", | |
"main": "index.js", | |
"scripts": { | |
"start": "functions-framework" | |
}, | |
"dependencies": { | |
"@google-cloud/functions-framework": "^1.2.1" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Greeter { | |
#name = 'Functions Framework'; | |
get name() { | |
return this.#name; | |
} | |
set name(name) { | |
this.#name = name; | |
} | |
sayHello() { | |
return `Hello, ${this.#name}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Greeter = require('./greeter'); | |
exports.function = (req, res) => { | |
const g = new Greeter(); | |
// Can't access private fields | |
// greet.#name = 'NewName'; | |
res.send(g.sayHello()); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.users = (req, res) => { | |
// Get the first part of the URL | |
switch (req.url.split('/')[1]) { | |
case '': getAllUsers(req, res); break; | |
case 'user': getUser(req, res); break; | |
default: getDefault(req, res); | |
} | |
res.send(req.url); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getAllUsers(req, res) { | |
// TODO: Get users from a database | |
res.send(['Alice', 'Bob']); | |
} | |
export function getUser(req, res) { | |
// TODO: Get user details | |
res.send({ name: 'Alice', location: 'LAX', }); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
// Create an Express object and routes (in order) | |
const app = express(); | |
app.use('/users/:id', getUser); | |
app.use('/users/', getAllUsers); | |
app.use(getDefault); | |
// Set our GCF handler to our Express app. | |
exports.users = app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://apis.google.com/js/api.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Hello, GAPI</title> | |
</head> | |
<body> | |
Hello, GAPI! | |
<script src="https://apis.google.com/js/api.js"></script> | |
</body> | |
</html> |