NodeJS #1
Інтро
$ whoami
Inna Ivashchuk
Senior Software Engineer
JS developer, music fan, movie-dependent and Star Wars fan 🤓
May the Force be with you!
Agenda
-
What is Node.js
-
How Node.js works
-
When to use Node.js
-
Node.js installation. NVM
-
Package manager: NPM / Yarn
What is ?
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
What is NodeJS?
What is V8 JavaScript engine?
How V8 works?
V8 JS Engine
Open-source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly
-
Heap Memory allocation
-
Call stack execution context
-
Orinoco Garbage Collector
-
TurboFan Optimization controller - takes this bytecode and generates machine code from it. (JIT)
-
Ignition JS Interpreter - generates bytecode from this syntax tree using the internal V8 bytecode format.
-
Liftoff WebAssembly
libuv
libuv is a multi-platform C library that provides support for asynchronous I/O based on event loops. It supports epoll, kqueue, Windows IOCP, and Solaris event ports. It is primarily designed for use in Node.js but it is also used by other software projects.
Network I/O
File I/O
DNS
66.8% of developers rated Nodejs as the most loved tool in Stackoverflow Developer Survey Report 2020
85% of Nodejs developers use it for web app development, and the other 43% use it for enterprise-grade applications
51.4% of developers regarded Nodejs as the most-used tool earlier in the 2020
How works
JavaScript is “single-threaded” because JavaScript works on “Event Loop”
NodeJS is NOT “single-threaded” as it has a libuv threadpool in its runtime.
The chefs are the libuv threadpool and OS Async Helpers.
The waiter is the V8 engine and the Event Loop
The JavaScript code you want to be executed is the food
Node.js restorant
Comparison with other platforms
w3techs.com statistic
The following server-side programming languages are used by less than 0.1% of the websites
w3techs.com statistic
The following server-side programming languages are used by less than 0.1% of the websites:
Miva Script
Lasso
C
Lisp
C++
Smalltalk
Tcl
Haskell
Go
Ada
Strong static typing
Dynamic typing / weak static
Event-loop / single threaded
Threads
Mandatory exception handling
Optional exception handling
OOP / Patterns
Proto / FP / Reactive/ NonBlocking
Dynamic typing / weak static
event loop
sync execution (threads) / reactive libs
Optional exception handling
Used by 79.1% of all websites (w3techs)
Strong static typing
Threads
Mandatory exception handling
OOP / Patterns
Advantages of using Node.js
Ability to use the same language on the client and server-side
Speed of work
The lightness of the system allows us to create lightweight applications
The opportunity to use JavaScript syntax
A Node package manager that you can use to install and search for packages
Constant technology development (e.g., TypeScript is being developed now)
Single thread Event loop to process requests
Disadvantages of Node.js
Poor of processing large data volumes
Low CPU-handling capacities
Node.js vs JS problem
- Inconsistent models
- Lack of semantic standardization
- Frequent changes
Messy syntax
When to use Node.js
Here are some questions to answer
What kind of project are we dealing with?
What kind of product we expect to deliver?
What is the project's reach?
What resources do we currently have?
What is the situation on the market?
What are our main performance criteria?
The main areas, where Node.js is more suitable:
Backend for Front-end
Enterprise web-applications
SSR (Server side rendering)
Message Servers and Event Broadcasting (Chats, Games, Interactive)
Build tools (webpack, babel and etc)
Desktop web-application (Electon: VS Code, Slack)
Microcontrollers
What is Node.js used for?
The environment can support intense traffic of multiple short messages or chatrooms in real-time messaging
Real-time application
Ability to process real-time flows - benefited from its event-driven, non-blocking model
Collaborative tools
The term “streaming” means exactly that – sending large amounts of data in smaller packages instead of a single batch. Node.js is perfectly suited for this task with built-in modules supporting data streaming and allowing to creation of both readable and writable data streams.
Data streaming applications
The superb scalability supported by Node.js answers the “why Node.js” question for apps required to withstand high peak loads.
Applications relying on scalability
Node.js installation. NVM
Node.js installation
LTS vs Current
Releases that receive Long-term Support, with a focus on stability and security.
Under active development. Code for the Current release is in the branch for its major version number
NVM
nvm stands for Node Version Manager. As the name suggests, it helps you manage and switch between different Node versions with ease. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.
Package manager: NPM/Yarn
npm package manager
- the website
- the Command Line Interface (CLI)
- the Registry
npm init
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
npm install
{
"name": "user-service",
"version": "0.0.1",
"description": "",
"author": "",
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
...
},
"dependencies": {
"@nestjs/common": "^7.6.15",
"@nestjs/core": "^7.6.15",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^7.6.15",
...
},
"devDependencies": {
"@nestjs/cli": "^7.6.0",
"@nestjs/schematics": "^7.3.0",
"@nestjs/testing": "^7.6.15",
...
}
}
package-lock.json
{
"name": "user-service",
"version": "0.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@angular-devkit/core": {
"version": "11.2.6",
"resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-11.2.6.tgz",
"integrity": "sha512-3dA0Z6sIIxCDjZS/DucgmIKti7EZ/LgHoHgCO72Q50H5ZXbUSNBz5wGl5hVq2+gzrnFgU/0u40MIs6eptk30ZA==",
"dev": true,
"requires": {
"ajv": "6.12.6",
"fast-json-stable-stringify": "2.1.0",
"magic-string": "0.25.7",
"rxjs": "6.6.3",
"source-map": "0.7.3"
},
"dependencies": {
"rxjs": {
"version": "6.6.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz",
"integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==",
"dev": true,
"requires": {
"tslib": "^1.9.0"
}
},
...
Versioning
- A normal version number MUST take the form X.Y. X is the major version and Y is the minor version.
- Minor version MUST be incremented for any release which maintains backwards compatibility to the public API.
- Major version MUST be incremented if any backwards incompatible changes are introduced to the public API.
- When compatibilty with SemVer tooling is required, it is acceptable to add a ‘.0’ patch version, e.g. 2.1.0, but normally that should be avoided.
npm publish
Publishes a package to the registry so that it can be installed by name
npm publish
Use a .npmignore file to keep stuff out of your package. If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file.
Yarn
Q & A
NodeJS Core #1
By Inna Ivashchuk
NodeJS Core #1
- 495