current position:Home>Node. js< I & gt—— Encounter node and repl usage

Node. js< I & gt—— Encounter node and repl usage

2022-04-29 18:45:24Running53

brief introduction

Node.js It's based on V8 JavaScript Engine JavaScript Runtime environment

Browser kernel

in fact , We often say that the browser kernel refers to the typesetting engine of the browser

Layout engine , Also known as browser engine 、 Page rendering engine or template engine

How the rendering engine works

stay HTML If you encounter script label , Then the browser will stop parsing HTML, Instead, load and execute JavaScript Code

that , Why not load and execute directly and asynchronously JavaScript Code , Not to stop HTML The analysis of ?

because JavaScript The code can operate our DOM, The browser wants to HTML Analytic DOM and JavaScript After the operation DOM Put it together to produce the final DOM Trees , Instead of frequently generating new DOM Trees

JavaScript engine

Why JavaScript And the engine ?

  • In fact, we wrote JavaScript Whether the code is handed over to the browser or Node To carry out , In the end, they all need to be CPU To deal with the
  • however CPU Only know machine language , So we need JavaScript The engine helps us to JavaScript The code is first translated into assembly language , Then it can be translated into machine language

JS The relationship between engine and browser kernel

Here we start with Webkit For example ,Webkit In fact, it consists of two parts

  • WebCore: be responsible for HTML analysis 、 Layout 、 Rendering and so on
  • JavaScriptCore: analysis 、 perform JavaScript Code , In the wechat applet JS The code uses JsCore To perform the

V8 engine

  • V8 Yes, it is C++ Compiling Google Open source high-performance JavaScript and WebAssembly engine , It is used for Chrome and Node.js
  • V8 The engine is cross platform
  • V8 Can run independently , It can also be embedded in any C++ In the application

Node.js What is it? ?

in other words Node.js Is based on V8 Engine to execute JavaScript Code , But it's not just V8 engine

  • Whether it's Chrome still Node.js, In fact, it's all embedded V8 Engine to execute JavaScript Code
  • But in Chrome Browser , It also needs to be parsed 、 Rendering HTML、CSS And other related rendering engines , In addition, you also need to provide support for browser operation API、 Browser's own event loop, etc
  • in addition , stay Node.js We also need some additional operations , Like file system reading / Write 、 The Internet IO、 encryption 、 Compress and decompress files and other operations

The browser and Node.js The difference in Architecture

Node.js framework

Node.js Not simply by js To write , Its different modules use different languages , such as V8 The engine uses C++ Compiling ,Node Some of the corresponding API Again JS To write the ,LIBUV This library is made up of C Written in language , So we can't simply say Node It is written in a certain language

Node.js Application scenarios of

  • At present, the front-end development library is based on node Managed in the form of packages
  • npm( install node It will be installed automatically npm)、yarn Tools become the most used tools for front-end development
  • More and more companies are using it Node.js Develop as a server
  • A large number of projects need the help of Node.js Complete the isomorphic reference of front and back rendering
  • Senior front-end engineer needs to write scripting tools for the project
  • A lot of companies are using Electron To develop desktop applications ( such as Vscode, It is based on Node.js and Chromium

Node Version tool for ( The following tools Mac To support ,Windows Is not supported , But support nvm-window)

  1. install n: Use it directly npm Can be installed
  • npm install -g n
  1. Install the latest lts edition
  • Previously added sudo It's a matter of authority
  • Multiple versions can be installed at the same time , And then we can go through n Quickly switch back and forth between two versions
    • n lts( Install the latest lts edition )
    • n latest ( Install the latest version )

Node Of REPL

What is? REPL Well ?

  • REPL yes Read-Eval-Print Loop For short , Translated into “ Read - evaluation - Output ” loop
  • REPL It's a simple one 、 Interactive programming environment

In fact, you are on the browser console or open node Writing code on the command line in the environment is REPL

copyright notice
author[Running53],Please bring the original link to reprint, thank you.

Random recommended