Skip to content

Instantly share code, notes, and snippets.

View gregfenton's full-sized avatar

Greg Fenton gregfenton

View GitHub Profile
@gregfenton
gregfenton / README.md
Last active March 10, 2023 17:43
Express: Start and write to console

ExpressJS: Start, Listen and Console.log()

Prerequisites

  1. Initialized npm project with package.json file. See: Getting started with npm.
  2. express installed as project dependency.
  3. Entry page (i.e. server.js) present in the project root.

Example directory structure

project-root
@gregfenton
gregfenton / README.md
Last active March 10, 2023 17:45 — forked from acidtone/README.md
Express: send() vs json() vs end()

Express: res.send() vs res.json() vs res.end()

Materials

Key Takeaways

  • When in doubt, use .send(). It dynamically sets Content-Type headers to match the data it sends.
  • When sending JSON, you can either use .json() or .send().
    • .json() is likely less confusing
    • .json() uses .send() under the hood so the resulting HTTP headers are the same.
@gregfenton
gregfenton / README.md
Last active March 10, 2023 03:33 — forked from acidtone/README.md
Express: Hello World!

Express: Add "Hello World!" API

Prerequisites

  1. Initialized ExpressJS project as per this gist

Example directory structure

project-root
    ├── node_modules
 ├── package-lock.json
@gregfenton
gregfenton / AuthProvider.js
Created February 8, 2022 03:18
A React/React-Native authentication provider that uses the Context API to make authentication & profile info available to "the rest of the app"
import React, {createContext, useEffect, useState} from 'react';
import auth from '@react-native-firebase/auth'; // or from 'firebase/auth';
import Loading from '../components/Loading';
import {myFirestore} from '../common/firebase.js';
export const AuthContext = createContext({});
export const AuthProvider = (props) => {
@gregfenton
gregfenton / FirestoreBackup.js
Last active December 7, 2022 22:57
Backup your Firestore db to a Firebase Storage bucket -- to be called from a Firebase Cloud Function (JavaScript/node)
const functions = require('firebase-functions');
const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();
//
// Add config values with CLI command:
// firebase functions:config:set my_app_name='{"fsBackupBucket":"gs://my-fs-backup-bucket"}'
//
const config = functions.config();
@gregfenton
gregfenton / AboutAppView.js
Created January 12, 2022 15:32
An "about app" page that displays various Expo constants
import React, {useContext, useState} from 'react';
import {StyleSheet, Dimensions, View, Platform} from 'react-native';
import {List, Button, Text} from 'react-native-paper';
import * as Sentry from '@sentry/react-native';
import * as Updates from 'expo-updates';
import Constants from 'expo-constants';
import {
widthPercentageToDP as wPct,
@gregfenton
gregfenton / eas_sync_customer_config.bash
Last active January 4, 2022 20:13
a shell script to clone a git repo into the current EAS build container
#!/bin/bash
set -e # exit when any command fails
function LOG() {
printf 'LOG: %s %s\n' "$(date '+%b-%d %T')" "${1}"
}
function CLONE_GITHUB_REPO() {
#
# EAS_SCRT_GITHUB_CONFIG_REPOS_TOKEN is an account-level Secret stored in our Expo account's Settings
import React from "react";
import Login from "../components/Login";
import Landing from "../Pages/Landing";
function Home(props) {
const [user, setUser] = useState();
return (
<div>
{user ?
@gregfenton
gregfenton / simple_useEffect_and_async.js
Last active February 26, 2021 17:24
REACT: A simple example of doing asynchronous work inside of a useEffect()
/**
* A React component:
* r1. name starts with a Capital letter
* r2. gets a single argument, typically referred to as "props", that is an object containing all properties passed in
* r3. can use hooks (useState, useEffect, useRef, etc...)
* r4. returns JSX
**/
/** r1. name starts with a Capital letter */
/** r2. gets a single argument, typically named "props" */
@gregfenton
gregfenton / firestore_add_field_to_all_docs_in_collection.js
Last active December 7, 2023 15:33
Updates all docs in a Firestore collection with the given values -- command-line JavaScript/node
/**
* firestore_add_field_to_all_docs_in_collection.js
*
* This script is assumed to live in an existing Javascript project that has its own package.json.
* I store this script in <PROJECT_ROOT>/tools/cleanup/.
*
* To use:
* 1. Import the 'firebase' and 'esm' NPM modules to your project (Run the command: `npm install firebase esm`)
* 2. Edit the data for USER1 and FIREBASE_CONFIG, then adjust parameters in `main` for the call to `visitDocs`.
*