Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
lejonmanen / ask.js
Created October 24, 2024 15:35
Read input from user in terminal, Node.js
/*
Use this file in your project.
But first, create a Node project:
npm init -y
Then add a line to package.json:
"type": "module",
Usage:
@lejonmanen
lejonmanen / .env
Last active September 26, 2024 15:04
Fullstack MERN with TypeScript
# Add your secrets here
CONNECTION_STRING=yes-here
@lejonmanen
lejonmanen / mongoose-node.ts
Last active September 13, 2024 12:59
Mongoose example with TypeScript
import { Schema, model, connect, Mongoose } from 'mongoose'
// Animal is an interface describing animal objects
import { Animal } from './animal.js';
const animalSchema = new Schema<WithId<Animal>>({
_id: ObjectId, // from WithId
species: String, // from Animal
factoid: String, // from Animal
score: Number // from Animal
@lejonmanen
lejonmanen / example.js
Created May 13, 2024 19:57
Example Cypress config for Vite+React
// This example produced 2024-05-13
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
@lejonmanen
lejonmanen / fire.js
Last active April 15, 2024 18:41
Firestore CRUD from static frontend 2024
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getFirestore, collection, addDoc, getDocs, getDoc, doc, updateDoc, deleteDoc } from 'firebase/firestore/lite'
// Get this from Firebase Console
import { firebaseConfig } from "./firebaseConfig.js";
// Initialize Firebase and get database
const app = initializeApp(firebaseConfig);
const db = getFirestore(app)
@lejonmanen
lejonmanen / App.jsx
Last active February 29, 2024 16:57
Conditional rendering exercise
/*
The Budget component uses "conditional rendering" to do
different things based on its props. What will each line
in the App component render?
*/
const App = () => (
<main>
<Budget value={5500} />
<Budget value={240} />
@lejonmanen
lejonmanen / Aaa.jsx
Last active February 29, 2024 16:37
React components
// A component is a function that returns JSX
const Aaa = () => (
<div>
You can do any JSX in here
</div>
)
// Remember to export the component
// Most people have one component per file and use export default
export default Aaa
@lejonmanen
lejonmanen / example.js
Created November 24, 2023 13:38
Using fetch with try+catch
// Simple request
async function sendRequest(url) {
try {
const response = await fetch(url)
if( response.status !== 200 ) {
console.log('Request wasn't successful. Code: ' + response.status)
return null
}
const data = await response.json()
return data // success
@lejonmanen
lejonmanen / example.js
Last active November 24, 2023 13:08
Sync vs async
// Option 1: use a promise directly
let data;
function itTakesTime1() {
return new Promise((resolve, reject) => {
/* do something that takes time and produces a value */
resolve(value)
})
}
@lejonmanen
lejonmanen / better.js
Last active October 24, 2024 15:32
Using Node.js async readline
import readline from 'node:readline'
async function ask(query) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, tabSize: 4 });
return new Promise((resolve) => rl.question(query, (answer) => {
rl.close();
resolve(answer);
}));
}