Node.js is a JavaScript runtime environment based on the V8 JavaScript engine

That is, Node.js executes JavaScript code based on the V8 engine, but not just V8:

  • V8 can be embedded in any C ++ application, whether Chrome or Node.js, and in fact the V8 engine is embedded to execute JavaScript code
  • In Chrome, you also need to parse and render HTML, CSS and other related rendering engines, and also need to provide API support for browser operations, the browser’s own event loop, etc
  • In Node.js we also need to perform some additional operations, such as file system read/write, network IO, encryption, compression and decompression of files, etc

  • The JavaScript code we write goes through the V8 engine (written in C++) and through node.js Bindings to put tasks into the Libuv event loop
  • Libuv (Unicorn Velociraptor) is a library written in C
  • Libuv provides event loops, file system reads and writes, network IO, thread pools, and more

Node version classification

LTS version: This version is relatively stable and is recommended for online environments

Current version: The latest version of Node, which contains many new features

Node version tool

mac

  • nvm: Node Version Manager

  • N: Interactively Manage Your Node.js Versions

window

  • nvm-window

The use of n

#The installation
npm install n -g

#View the VERSION of N
n --version

#Install the LTS version
n lts

#Install the latest version
n latest

#Install the specified versionN < Version number >
#Remove a versionN rm < Version number >
#View all versions and switch between versions
n
Copy the code

If no permission is displayed during node version management using n, you can use the sudo command together with n

NVM - the use of the window

#The installation
npm install nvm-window -g

#View the installed Node version
nvm list

#View all available node versions
nvm list available

#The installationNVM install the latest | LTS | specific version number >
#removeNVM uninstall the latest | LTS | specific version number >
#Switch versionNVM use < latest | LTS | specific version number >Copy the code

If you use NVM-Window to install a certain version of Node, the installation speed is slow or fails

You can change the download address of NVM-Window node and NPM to the corresponding mirror address of Taobao

Run js files in the Node environment

index.js

console.log('Hello World')
Copy the code

perform

#Node 
      
node index.js
Copy the code

However, every time we modify the corresponding JS file, we need to re-execute the Node command to run the js file

If we want to listen for changes to js files in real time and execute them, we can use Nodemon

#The installation
npm install nodemon -g

#Execute the JS file
#Nodemon 
      
nodemon index.js
Copy the code

REPL

REPL is short for read-eval-print Loop, which translates as “read-evaluate-print” Loop **

REPL is a simple, interactive programming test environment

#Start the REPL environment for a node
node

#Type node and press Enter to enter the REPL environment of node
#Press twicecommandPlus C or oncecommand+ D recommends the REPL interaction environment for node
Copy the code

Pass parameters — input

console.log(process.argv)
// File execution mode -> node index.js Klaus env=production

/ * = > ['/usr/local/bin/node ', / / = > node command in the path '/ Users/Klaus/Desktop/node - demo/index, js', // the first two arguments are the default arguments, starting with the third argument, which is the argument 'Klaus ', 'env=production'] */ we passed when executing the file
Copy the code

The output of the node

  • The most common way to enter content is console.log
  • Clear the console: console.clear
  • Prints the call stack of the function: console.trace — Prints the stack of the line of code that executes the command

Other output methods can be viewed here

Global object

Node provides us with some global objects that we can manipulate

Special global object

These global objects can be used arbitrarily in modules, but not in command-line interactions

That is, these objects are not global in nature, but can be used in every module

In Node, every JS file is a module, so these objects can be used almost as global objects

These special global objects include: __dirname, __filename, exports, module, require

__dirname: Gets the path of the current file – not including the following file name

__filename: Gets the path and filename of the current file, including the following filename

console.log(__dirname)
console.log(__filename)

/* => /Users/klaus/Desktop/node-demo /Users/klaus/Desktop/node-demo/index.js */
Copy the code

Common global objects

  1. Process object: Processes provide information about Node processes

    • For example, the running environment and parameter information of Node
    • We can also read some environment variables into the process env
  2. Console object: Provides a simple debugging console

  3. Timer functions: There are several ways to use timers in Node

    • SetTimeout (callback, delay[,…args]): Callback executes once after delay milliseconds — corresponding to clearTimeout
    • SetInterval (callback, delay[,…args]): Callback is executed once every delay millisecond, corresponding to clearInterval
    • SetImmediate (callback[,…args]): The “immediate” execution of a callback after a callbackI/O event – corresponding to cleatImmediate
    • Process.nexttick (callback[,…args]): adds to the nextTick queue
setTimeout(() = > console.log('Third execution'), 1000)
setInterval(() = > console.log('Fourth execution'), 1000)
setImmediate(() = > console.log('Second execution'))
process.nextTick(() = > console.log('First execution'))
Copy the code
  1. Global is a global object. In fact, the process, console, setTimeout mentioned in the front end are all put into global
    • Similar to the Window object in the browser
    • Print the Global object directly, printing only a portion of its properties
    • If you want to output all of the properties in Global, you need to do so in node’s REPL environmentglobal.+ TAB twice

The difference between Global and Window

  1. The mounted properties are different

    • In browsers, global variables are on Windows, such as Document, setInterval, setTimeout, alert, console, and so on

    • In Node, we also have a global attribute, which does not have window, document, etc., but adds fs, HTTP, etc

  2. Variables defined by var are not mounted to global objects

    • Variables defined in the browser using the var keyword are mounted by default as properties of the Window object
    • In Node, every JS file is a module, so variables defined using var in Node can only be the variables of the current module, but cannot be mounted to the global object global
var name = 'Klaus'

console.log(name) // => Klaus
console.log(global.name) // => undefined
Copy the code