Skip to content

Instantly share code, notes, and snippets.

View grant's full-sized avatar
Coding daily!

Grant Timmerman grant

Coding daily!
View GitHub Profile
@grant
grant / index.js
Last active July 20, 2019 01:45
GCF Debugging VSC List Google APIs
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
@grant
grant / Dockerfile
Created August 3, 2019 01:32
Run Node 12 Dockerfile
# 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.
@grant
grant / package.json
Created August 3, 2019 02:26
Run Node 12 package.json
{
"name": "ffnode12",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "functions-framework"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.2.1"
}
@grant
grant / greeter.js
Created August 3, 2019 03:23
Run Node 12 greeter
class Greeter {
#name = 'Functions Framework';
get name() {
return this.#name;
}
set name(name) {
this.#name = name;
}
sayHello() {
return `Hello, ${this.#name}`;
@grant
grant / index.js
Last active August 3, 2019 04:02
Run Node 12 index.js
const Greeter = require('./greeter');
exports.function = (req, res) => {
const g = new Greeter();
// Can't access private fields
// greet.#name = 'NewName';
res.send(g.sayHello());
};
@grant
grant / index.js
Last active August 27, 2019 17:04
Express Routing: Switch
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);
};
@grant
grant / index.js
Last active August 27, 2019 17:09
Express Routing: Switch (2)
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', });
}
@grant
grant / index.js
Last active August 27, 2019 17:27
Express Routing: Router
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;
@grant
grant / index.html
Created August 30, 2019 18:46
gapi_start
<script src="https://apis.google.com/js/api.js"></script>
@grant
grant / index.html
Created September 2, 2019 21:37
gapi HTML
<!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>