Node: fs common uses

Official Doc: File System API const fs = require('fs'); Append to a file (if file doesnt exist, create it) Useful when need to log to a file. fs.appendFile('message.txt', 'data to append', function (err) { if (err) throw err; console.log('Saved!'); }); Write a file const fs = require('fs'); fs.writeFile("/tmp/test", "Hey there!", function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); }); // Or fs.writeFileSync('/tmp/test-sync', 'Hey there!'); Read a file one line at a time Since Node....

May 10, 2022 · 2 min · Anant

Day4 #100DaysOfCode

Build $ npm run build generates static pages. Terminal: Page Size First Load JS ┌ ● / 724 B 78.2 kB ├ └ css/233a1356c16eb650.css 416 B ├ /_app 0 B 77.5 kB ├ ○ /[noteId] 484 B 78 kB ├ └ css/f0a2be9a550dc29f.css 79 B ├ ○ /404 193 B 77.7 kB └ ○ /new-note 749 B 78.2 kB └ css/29ba8ee8d48d1f49.css 359 B + First Load JS shared by all 77.5 kB ├ chunks/framework-1f10003e17636e37....

May 10, 2022 · 4 min · Anant

Day3 #100DaysOfCode

Replace mock data with a real datasource: Send HTTP request once the page has loaded - we would typically use useEffect() Runs whenever component is frist rendered, never after. Do: fetch your data inside it. Manage state for the component - use useState() hook const [loadedNotes, setLoadedNotes] = useState([]); useEffect(()=> { //send http request and fetch data. //set on component setLoadedNotes(LOADED_NOTES); }, []); Full code (with function component): function HomePage(){ const [loadedNotes, setLoadedNotes] = useState([]); useEffect(()=> { //send http request and fetch data....

May 9, 2022 · 3 min · Anant

Day 2 #100DaysOfCode

Day 2 Key Learnings: pages folder name is reserved in nextjs. Others like components is not reserved. How props are passed to a component. Utilize _app.js for anything that affects all pages like Layout - wrap with Layout component, Navigation (with links) etc Using Programmatic (Imperative) Navigation The usual rule of a Reach Hook: Only use directly only at the root level of component function. (e.g. using useRouter) useRouter has not just query that gives slug, it also has push() whose job is equivalent to Link component Starting point of project with some templates...

May 8, 2022 · 6 min · Anant

Deploy to Heroku

The most updated documentation to get started with Heroku is the official Heroku Dev Center documentation. Following is to get started with Node.js on Heroku 1. Install git and perform first time git setup Git installation First-time Git setup 2. Install heroku cli from here 3. Login in heroku using heroku cli heroku login On successful authentication, you get success message. Logging in... done Logged in as xxxxxxxx@xxxxx.com Prepare the app If dont already have the project in your local, clone it from your remote:...

May 5, 2022 · 5 min · Anant