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?


Basic Concepts

<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 lineStateful: 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

  1. Client-Server: both parts do not know everything about each other
  2. Stateless: server does not store the state or sessions of the requests
  3. Cache: it is not implemented at the beginning but it is important to have it
  4. Uniform Interface: recurses identification (https//:example/products), recurses representation (JSON, XML, HTML…), auto descriptives messages (errors, success)
  5. HATEOAS (Hypertext As The Engine Of Application State): return links
  6. Layers: load balancer, security
  7. On-demand code

</aside>


REST / HTTP methods

<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>


Params Requests

<aside> 🗣 Route Params: is used as a filter: ?theme=React → request.query

Query Params: is to identify a recurse: user/:id → request.params

</aside>


FileSystem

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

Path

import path from 'path';

//resolve path file, __dirname is where you are
path.resolve(__dirname, '..', 'tmp');