NodeJS

Because there are too many articles about this kind of introduction on the Internet, I recommend a few relevant introductions and extract some keywords. We can simply understand some of them. If you want to go further, you can search related resources on the Internet by yourself

: Thumbsup: Related articles recommended

  • Wikipedia: Introduction to Nodejs
  • Baidu Encyclopedia: Nodejs introduction

Point_down: Talking about personal understanding

First, take a look at the description on the Nodejs website

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

Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient

Extract key

  • Nodejs is a ==JavaScript runtime environment ==, which provides a platform for JavaScript code to run
  • Nodejs uses the == event-driven, non-blocking I/O model ==
  • Nodejs benefits: Lightweight and efficient
  • For those of you who are new to the event driven and non-blocking I/O model, you don’t have to worry about what the event-driven and non-blocking I/O models are

Nodejs installation

Nodejs installation is relatively easy, so the following installation tutorial is recommended to successfully install NodeJS

Rookie tutorial: NodeJS installation

After the installation is complete, enter node -v on the cli to view the current nodeJS version. Since my local nodeJS version is V8.11.3, it is recommended that you install the same nodeJS version to avoid unforeseen problems

After nodeJS is installed, it will come with another tool to help us install NPM (Node Package Manager).

NPM is a nodeJS package management tool, which can be used to manage the nodeJS module package easily

It is also suggested that students who are new to NPM do not have to worry about it, but follow the notes to use it step by step, so that they can understand what NPM is and what convenience it brings to our development. If you want to know more about it, two links are recommended here

  • NPM Chinese document
  • Wikipedia: Introduction to NPM

NPM command

The following are common NPM commands, which may look familiar to you before you use them

The first five commands I’ve highlighted in bold are the ones we use most often during development

  1. **npm -v: ** Check the NPM version.
  2. **npm init: ** One will appear after initializationpackage.jsonConfiguration file. You can add it to the end-yTo quickly skip the question-and-answer interface.
  3. **npm install: ** will be based on the projectpackage.jsonFiles automatically download all dependencies required by the project.
  4. **NPM install --save-dev(NPM install Package name -d) : ** Installed packages are for development only, not production, and will appear inpackage.jsonIn the filedevDependenciesAttribute.
  5. **NPM install package name --save(NPM install Package name -s** Installed packages that need to be published to production will appear in package.json filesdependenciesAttribute.
  6. npm list: View the installed Node packages in the current directory.
  7. npm list -g: View node packages that have been installed globally.
  8. npm --help: View the NPM help command.
  9. NPM update package name: Updates the specified package.
  10. NPM uninstall the package name: Uninstalls the specified package.
  11. npm config list: Displays the configuration information.
  12. NPM specifies the command --help: Displays the help information about a specified command.
  13. NPM info specifies the package name: Displays all version information about the specified package on the remote NPM.
  14. npm config set registry https://registry.npm.taobao.org: Changes the package download source. In this example, the package is changed to taobao image.
  15. npm root: Displays the installation path of the current package.
  16. npm root -g: View the installation path of the global package.
  17. NPM ls package name: Displays information about the specified package and version installed locally. Empty is not displayed.
  18. NPM ls Package name -g: Displays the specified package and version information of the global installation. Empty is not displayed.

The concept of modularity

Node.js adopts the CommonJs specification. In NodeJS, the code is generally divided into different JS files, each file is a module, and the file path is the module name.

Each module is written with three predefined variables: require, exports, and Module.

4.1 Details

  • **require: ** Used to load and use other modules in the current module, passing in a module name and returning a module export object. Module names can be relative paths (starting with./) or absolute paths (starting with a drive letter such as/or C:). In addition, the.js extension in the module name can be omitted. Here’s an example.

    var person1 = require("./person");
    var person2 = require("./person.js");
    Copy the code
  • Exports: Exports objects are exports of the current module, used to export module public methods and properties. Other modules that use the current module through the require function get the exports object of the current module. The following example exports a public method.

    exports.hello = function(){
        console.log("hello world!");
    }
    Copy the code
  • Module: The Module object provides access to some information about the current module, but is mostly used to replace the exported object of the current module. For example, the module export object is a normal object by default. If you want to change it to a function, you can use the following method.

    module.exports = function(){
        console.log("Replace the current modular export object")}Copy the code
    • In the above code, the module’s default export object is replaced with a function.

4.2 Module initialization and main module

** Module initialization: ** THE JS code in a module is executed only once, the first time the module is used, and initializes the module’s exported objects during execution. The cached exported objects are then reused.

** Master module: ** Modules that are passed to NodeJS with command-line arguments to start the program are called master modules. The main module is responsible for scheduling the other modules that make up the entire program to complete their work. For example, main.js is the main module when the program is launched with the following command.

node main.js
Copy the code

4.3 the Demo sample

Under the sibling directory, create counter. Js, main.js, as shown below

//counter.js

var i = 0;

function count() {
    return ++i;
}

exports.count = count;
Copy the code
// main.js
var counter1 = require('./counter');
var counter2 = require('./counter');

console.log(counter1.count());
console.log(counter2.count());
console.log(counter2.count());
Copy the code

Run the main module with the node command to run the tests:

  • No matter how many times counter is initialized, it’s the same counter object

In live.

  • NodeJS is a JS script parser. Installing NodeJS on any operating system essentially copies the NodeJS executable to a directory, and then ensures that this directory is under the system PATH environment variable so that it can be used on the terminalnodeCommand.
  • Enter directly from the terminalnodeThe command goes into command interaction mode, which is ideal for testing someJSCode snippets, such as regular expressions.
  • NodeJSuseCMDModule system, the main module as the program entry point, all modules in the execution of the initialization only once.
  • Use the NPM tool to manage various package modules in NodeJS