Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Official website: https://nodejs.org/en/
🔍 What do you find here?
<aside> 📌 ❯ Event loop architecture: coordinate the tasks and callbacks executions. The event loop listens to Call Stack (the functions are here), and one function each time, then put in a thread. It is a pile, so the function that is joined last is the first one to go out. ❯ Single Thread: the event loop is a single thread, but Node by default has 4 threads, coordinated by event loop ❯ Non-blocking I/O: does not block a method if another one is running ❯ Modules: http, dns, fs, buffer ❯ process.argv: get arguments from the command line ❯ Stateful: data in memory vs Stateless: data in DB ❯ Middleware: intercepts requests, do something and next()
</aside>
<aside> 📢 ❯ API: Application Programming Interface - rules to specify interactions with the application
❯ REST: Representation State Transfer - architecture model ❯ REST Rules
</aside>
<aside> 🧨 ❯ GET: just read ❯ POST: to create a data ❯ PUT: to update a data ❯ PATCH: to update partially a data ❯ DELETE: to delete a data
Status Code ❯ 1xx: informative ❯ 2xx: success ❯ 3xx: redirect ❯ 4xx: client error ❯ 5xx: server error
</aside>
<aside> 🗣 Route Params: is used as a filter: ?theme=React → request.query
Query Params: is to identify a recurse: user/:id → request.params
</aside>
import fs from 'fs';
fs.writeFile('test.txt', 'append', function()) // append on file
fs.writeFile('test.json, JSON.stringify(FOO), function()) // write on file JSON
fs.writeFileSync('test.txt, 'foo', function()) // write file sync
fs.readFile(test.txt', 'utf-8', function()) // read file
JSON.parse(fs.readFile('test.json)) // read JSON file
fs.readFileSync('test.txt', 'utf-8', function()) // read file sync
fs.appendFile('test.txt', 'append', function()) // append on file
import path from 'path';
//resolve path file, __dirname is where you are
path.resolve(__dirname, '..', 'tmp');