Open adrisantos11 opened 1 year ago
Introduction to Node.js (videos from 1-7)
Summary:
Resume: Important to understand what is Node, where it cames from and how it works.
Local modules (videos from 8 to 17)
Summary:
module.export = { ... }
(10-11)(function(exports, require, module, __filename, __dirname) { ...code })()
(12)module.exports func/class/...
, module.exports = () => { ... }
, module.exports { ... }
, exports.func1; exports.func2;
) (14)module.exports
and exports
: module.exports
is the real function and exports
is just a memory reference to module.exports
(15).mjs
as file extension to use ES Modules and the way to use it is by export default
or export
and import it as import * from './rute'
(16)
Good to know: .ts
files and .tsx
files have the ES Modules implemented, but if you want to use this type of module systemusing javascript native code, each file extension should be .mjs
Resume:
module.exports
or exports
and imported with require()
.
b. ES Modules: Exported with export default
or export
and imported with import * from './rute'
node --watch file
(17)Build-in Modules (18 to 36)
Summary (also summary on video 36 - 2:00)
Extending from EventEmitter. Possible to create a class extending from EventEmitter and use its methods to create events inside the custom class (22)
Fs Module: Read and write into file synchronously and asynchronously (26)
How does fs-module work with stream data? (28) Types of streams (28):
- Readable streams => Ex: reading from file
- Writable streams => Ex: writing to a file
- Duplex streams (readable and writable) => Ex: Sockets
- Transform streams (modify/transform data while is written/read) => Ex: file comrpession where ayou can write compressed data and read de-compressed data to and from a file as a transform stream.
Fs Promises Module: Read and write a file synchronously and asynchronously with promises. In terms of performance is better to use fs-module instead of fs-promise-module, but for better code understanding, is better to use the fs-promise-module (27)
HTTP Module: How does the web work. HTTP (Hypertest Transfer Protocol) explanation (30)
- Create an HTTP server in Node.js (31)
- JSON Response (32)
- HTML Response. Reading an HTML file and return its data. Using of buffer and chunks to updgrade the performance of reading. (33)
- HTML Template. In case of adding dynamic variables (34)
- HTTP Routing. Request url with ifs and switch (35)
- Back-end wen frameworks. Express, Nest, Hapi, ... (36)
Needed knowledge
function func(callback) { callback(); }
.
Types of callbacks: syncrhonous callbacks (executed inmidiatly) and asynchronous callbacks (usually used to continue or resume after an asynchronous operation)
Node.js have asycrhonoys nature to prevent blocking of execution (20)
- Stream: Sequence of data that is being moved from an endpoint to another over the time.
- Buffer: Incredible example in the video (rollercoaster example => 2:15 - 5:05)
Resume
node:
protocol when importing built-in modulesLibuv dependency (37 to )
Summary
- What? Cross platform open source library written in C language.
- Why? Handles asynchronous non-blocking operations in Node.js.
- How? Using thread pool and event loop.
- Libuv's thread pool has 4 threads by default.
- Encrease the thread pool size (Windows via command line with
set UV_THREADPOOL_SIZE=5
).- Speed relation with CPU Cores. Performance is limited by the number of CPU cores.
Resume
Have a look to the next Node JS tutorial in Youtube: Node JS Tutorial 2023