node.js

Node.js overview

Node.js

Node.js is a JavaScript runtime built on the V8 JavaScript engine [1]

[1] :https://nodejs.org/docs/latest-v17.x/api/documentation.html

V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++ [2]

[2] :https://v8.dev/

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications. [3]

[3] : https://webassembly.org/

As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. In the following « hello world » example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep. [1]

This is in contrast to today’s more common concurrency model, in which OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node.js are free from worries of dead-locking the process, since there are no locks. Almost no function in Node.js directly performs I/O, so the process never blocks except when the I/O is performed using synchronous methods of Node.js standard library. Because nothing blocks, scalable systems are very reasonable to develop in Node.js. [1]

[1] : https://nodejs.org/en/about/

Asynchronous programming, conversely, is a multithreaded model that’s most applicable to networking and communications. Asynchronous is a non-blocking architecture, which means it doesn’t block further execution while one or more operations are in progress.

With asynchronous programming, multiple related operations can run concurrently without waiting for other tasks to complete. During asynchronous communication, parties receive and process messages when it’s convenient or possible to do so, rather than responding immediately upon receipt.

Texting is an asynchronous communication method. One person can send a text message and the recipient can respond at their leisure. In the meantime, the sender may do other things while waiting for a response.[5]

[5] : https://www.mendix.com/blog/asynchronous-vs-synchronous-programming/

Synchronous is known as a blocking architecture and is ideal for programming reactive systems. As a single-thread model, it follows a strict set of sequences, which means that operations are performed one at a time, in perfect order. While one operation is being performed, other operations’ instructions are blocked. The completion of the first task triggers the next, and so on.

To illustrate how synchronous programming works, think of a telephone. During a phone call, while one person speaks, the other listens. When the first person finishes, the second tends to respond immediately.[5] :https://www.mendix.com/blog/asynchronous-vs-synchronous-programming/

A thread is a single sequential flow of execution of tasks of a process so it is also known as thread of execution or thread of control. There is a way of thread execution inside the process of any operating system. [6]

[6] : https://www.javatpoint.com/threads-in-operating-system

Node.js pour faire simple, c’est un serveur en Javascript qui permet tout un tas de choses. Il peut être utilisé par exemple comme un serveur web, ou encore en local sur sa machine pour assister le développeur lorsqu’il créé un site ou une application. [2]

Il n’y a rien qui cloche dans ce que je viens dire ? Un serveur en Javascript vraiment ? On m’a toujours dit que JS c’était un langage de programmation côté client non ? [2]

Et bien si, tout à fait ! Mais lorsque son créateur a imaginé un environnement temps réel basé sur des événements pour en faire un outil passe-partout, il s’est dit que Javascript était le langage le mieux adapté pour ça ! [2]

Du coup on utilise aujourd’hui Node.js dans des cas très divers et variés. Il peut-être utilisé :

1.Comme serveur pour des applications web qui affichent des informations en temps réel (par exemple des applications de visio).

2.En local sur la machine du développeur pour compiler et transformer des fichiers lors de l’enregistrement

3.Pour gérer les dépendances d’un projet (toutes les librairies externes) grâce à NPM [2]

Pour synthétiser on peut dire que Node c’est du Javascript qui tourne sur un serveur dans une machine virtuelle. Et NPM permet de télécharger des librairies et gérer leurs dépendances. [2]

[2]: https://capitainewp.io/formations/wordpress-creer-blocs-gutenberg/node-npm-outils-librairies-necessaires/

chevron_left
chevron_right