NodeJS is introduced

What is NodeJS

  • Node.js is a JavaScript Runtime built on Chrome’s V8
  • Node.js uses an event-driveb, non-blocking I/O model

Non-blocking I/O

  • Blocked: The process hibernates during I/O and waits for the I/O to complete
  • Non-blocking: When I/O, the function returns immediately and the process does not wait for the I/O to complete

event-driven

  • Notification after an asynchronous operation such as I/O is complete
  • Observer model

Why NodeJs

  • A larger range of front-end responsibilities and a unified development experience
  • It has obvious advantages in processing high concurrency and I/O intensive scenarios

CPU intensive VS I/O intensive

  • CPU intensive: CPU actions (compression, decompression, encryption, decryption) such as computation and logical judgment are spent most of the time.

  • I/O intensive: most of the time is spent reading access devices, network facilities (file operations, network operations, databases)

    • Common Web Scenarios
      • Static resource reading
      • Database operations
      • To render the page

High concurrency solutions

  • Increase number of machines
  • Increased number of cpus per machine – multi-core (increased computing power)

Basic knowledge

  • process

    • A program in a computer is a running activity on a data set. It is the basic unit of resource allocation and scheduling in a system

    • Multi-process: Start multiple processes that can perform multiple tasks together

    • A single CPU can run multiple processes at the same time

      • Similar to the principle of animation, to the human eye can not perceive the speed of the play between multiple tasks (process) back and forth
  • thread

    • A relatively independent, schedulable unit of execution within a process that shares the resources of the process with threads belonging to the same process

    • Starting a process You can start multiple threads within a process, which can perform multiple tasks together

  • I/O classification and detailed explanation

  • NodeJs working model

    NodeJs working model

    • Only one process is enabled for a single thread, and only one thread is enabled for a process (i.e. only one process is enabled for a CPU, and only one thread is enabled for a process)
      • Single thread is only for the main process, the I/O operating system multithreading scheduling (asynchronous operations are part of the I/O operating system multithreading operations have nothing to do with NodeJS, NodeJS[the main process] initiates a request, sends the request to the I/O)
    • A single thread is not a single process.
  • Common scenario

    • Web scenario: High concurrency and I/O intensive
    • web server
    • Native code building
    • Utility development

Debug project & initialization

The environment

  • CommonJS (Module Specification)

    • Each file is a module and has its own scope (modules loaded are not loaded again, but directly take the result of the module loaded).
    • Inside the module, the Modeule variable represents the module itself
    • The module.exports property represents the module’s external interface
  • Global (no BOM, DOM, window)

    • Similar to the window
    • Buffer(binary data processing), process.console
    • timer
  • process

    • Process. NextTick micro tasks

      debugging

  • Inspector

  • VS Code

Based on the API

path

  • _dirname, _filename always returns the absolute path to the file
  • Process.cwd () always returns the folder where the node command was executed

. /

  • Always relative to the current file folder in the require method
  • As with **process.cwd()** elsewhere, start folders relative to Node

Buffer(directly mounted in global)

  • Buffers are used to process binary data streams
  • Instance is like integer array, fixed size (default hexadecimal, cannot change length)
  • C++ code allocates physical memory outside the V8 heap

Simple Web Server

Using the Supervisor file to listen for changes Using the Handlebars template engine using the **curl simulation request Web server (MAC) Using Commander or yargs** to get the command line argument list

Implement a static resource server

Run the command to change the local directory into the root directory of a static resource server. You can access the file Anywhere through the URL

Basic steps

  • To achieve the file read and write, and in the corresponding Host:port open
  • Set up the NPM configuration and upload it to the NPM generate NPM CLI

.gitignore

  • Before the matching mode, /: indicates the project root directory
  • A /: at the end of a matching pattern indicates a directory
  • Match pattern before add! : represents the inverse
  • * : indicates any character
  • ? : Matches any character
  • **: matches multi-level directories

.npmignore

.npmignore details

EditorConfig

Set range for the file

  • RequestHeader set
    • range: bytes=[start]-[end]
  • ResponseHeader set
    • Accept-Range: bytes
    • Content-Range: bytes start-end/total

Cache headers

  • responseHeader
    • Expirse (returns absolute time)
    • Cache-control (returns the relative time of the last request max-age)
  • If-modified-since/last-modified
    • requestHeader: If-Modified-Since
    • responseHeader: Last-Modified
  • If-none-match/ETag Request cache flow

The semantic version number is X.Y.Z

  • Update Z bit: Bug fix
  • Upgrade Y bit: new features and compatibility with previous and bug fixes
  • Upgrade X bit: Compatibility is not guaranteed (generally even is stable version)
  • x.y.*:
    • For example, 1.2.* : automatically updates z bits, and the latest bug fix is the same as ~1.2.0
    • X/y fixed
  • X and ^2.0.0

In Linux, you need to assign operation rights to files

Local building

Super easy version to build

  • The compression
  • Less/SCSS is built as CSS, ES5/ES6 is built as browser-recognized version, etc

gulp

babel

webpack

Unit testing & Release

Assertions library

Mocha

Example NodeJS crawlers

Simple climb baidu picture

  • A program that automatically captures network information according to certain rules (search engine)

    • Crawl to HTML
    • Cheerio converts HTML to a JQ-like object (you can use JQ operations to get information)
  • The crawler

    • User-agent, Referer, verification code
    • Number of visits between units, number of visits
    • Key information picture confusion
    • Asynchronous loading