What can Node do

  • Lightweight, high-performance Web services
  • Isomorphism development of front and back js
  • Convenient and efficient front section engineering

The architecture and running process of Node

  • The current layer content is implemented by JS
  • Provides applications that can call libraries directly, such as FS, PATH, HTTP, etc
  • The js language cannot operate the underlying hardware configuration directly

Builtin Modules Glue layer

Js and specific functional implementation of the control layer

The underlying

  • V8: Executes JS code and provides a bridge interface
  • Libuv: ⌚ Event loop, event pair column, asynchronous IO
  • Third-party modules: Zlib, HTTP, C-ares

Why NodeJS

1. Nodehs slowly evolves into a server ‘language’

IO is the slowest part of a computer operation

2.1 Reactor model: Single-thread completes multi-thread work

Suppose there are N people and other dishes, the normal multi-threading is multiple servers corresponding to multiple guests to order service

The Reactor model lets N people order their own food, and one person orders it at a time

2.2 Asynchronous I/O and event-driven in Reactor mode

2.3 NodeJS uses Libuv to process asynchronous IO, so it can handle IO intensive and concurrent requests well

2.4 Nodejs Asynchronous I/O

Against 2.4.1 IO

  • Blocking IO
  • Non-blocking IO
  • Repeatedly invoke the I/O operation to check whether the I/O operation is complete

Expect non-blocking IO without active judgment

Libuv library through the event-driven form to notify the end of IO, just need to listen for the END of IO event in the place of concern, you can not actively call to know the end of IO

2.4.1.2 Io summary

  • IO is the bottleneck of the application
  • Asynchronous I/O improves performance without waiting for results to return
  • IO Operating system level, platform has corresponding implementation
  • Nodejs single thread implements asynchronous IO with event-driven architecture and Libuv
  • Asynchronous I/O improves CPU usage

NodeJs event-driven architecture

1.1 Event-driven Architecture is a common pattern in software development.

The main form can be stated as the principal publishes the message and other instances receive the message

1.2 Nodejs single thread

  • Asynchronous I/o
  • event-driven
  • Event loop
  • These three methods are combined to form the Reator mode, in which a single thread does the work of multiple threads

1.3 NodeJs Uses Js to implement efficient, scalable, and high-performance Web services

Q: How does a single thread achieve high concurrency

  • Asynchronous non-blocking IO works with event callback notifications

Note that the main NodeJs programmer is single-threaded, while non-NodeJS is single-threaded, and Web workers have been added in V8

Nodejs application scenarios

2.1 IO intensive High Concurrency Requests

2.2 NodeJs as the middle layer

2.3 Operating the Database API services are provided

Const express = require('express') const app = express() app.listen(3000, () => {console.log(' service started on port 3000 ')})Copy the code

2.4 Live chat applications

Real-time chat applications can be easily implemented with socket. IO

NodeJs global object

  • Not exactly the same as Windows on the browser platform

  • Each module in NodeJs is wrapped in an IIFE function and is independent, so there is no this pointing to a global object

  • A number of properties are mounted on the NodeJs global object

1.1 The global object in NodeJs is global

  • The fundamental role of global is to act as a host
  • A global object can be thought of as a host for global variables

1.2 NodeJs Common global variables

  • __fileName

  • Returns the absolute path to the script file being executed

  • __dirName

  • Returns the directory where the script file is being executed

  • The timer function

  • The relationship between the execution order and the event loop

  • process

  • Provides an interface to interact with the current thread

  • require

  • Implement module loading

  • module\exports

  • Handle module exports

1.3 the process

  • Use it without requiring (all global variables, since nodeJS already injected global variables when running the script file)
  • Obtaining Process Information
  • Execute process operation

Common methods

  • Memortusage Memory for obtaining resource information

    { rss: 19070976, heapTotal: 3801088,

    heapUsed: 3018384, external: 327227, arrayBuffers: 9914 } memoryUsage

  • HeapTotal and heapUsed represent V8 memory usage.

  • External represents the memory usage of C++ objects bound to Javascript objects managed by V8.

  • RSS, or resident set size, is the size of the physical memory allocated to the process (a subset of the total allocated memory), including all C++ and JavaScript objects and code.

  • ArrayBuffers represents the memory allocated to ArrayBuffer and SharedArrayBuffer, including all Node.js buffers. This is also included in the external value. When Node.js is used as an embedded library, this value may be 0, because the allocation of an ArrayBuffer may not be tracked in that case.

  • cpuUsage

    { user: 37002, system: 24749 } cpuUsage

  • CWD run directory

  • Version node environment

  • Versions Sets the version information about the UV and V8 libraries in a node

  • The arch CPU architecture

  • Env. NODE_ENV User environment

  • Env. PATH Environment variable

  • Env. USERPROFILE/ env.HOME User root directory

  • Process. platform System platform

  • Process. argv startup parameter, PID, run time

  • Argv0 is passed by default

  • ExecArgv actively transmits parameters

  • pid

  • Uptime Running time

Path

Common API

1.1 the baseName

Gets the base name in the path

/ * * * 01 return is receiving path of the last part of the * 02 said the second parameter extension, if not set the return the full file name with the suffix * 03 second parameter for a suffix, if not in the current path is match to the, so will ignore the * 04 processing if the directory path, At the end of the path separator, */ /* console.log(path.basename(__filename)) console.log(path.basename(__filename), '.js')) console.log(path.basename(__filename, '.css')) console.log(path.basename('/a/b/c')) console.log(path.basename('/a/b/c/')) */Copy the code

1.2 dirname

Gets the path directory name

Log (path.dirname(__filename)) console.log(path.dirname('/a/b/c')). console.log(path.dirname('/a/b/c/')) */Copy the code

1.3 extname

Gets the extension name of the path

/** * 01 Returns the suffix of the corresponding file in the path * 02 If there are multiple points in the path, it matches the last point, */ /* console.log(path.extname(__filename)) console.log(path.extname('/a/b')) console.log(path.extname('/a/b/index.html.js.css')) console.log(path.extname('/a/b/index.html.js.')) */Copy the code

1.4 the parse

Parsing path

/** * 01 Receive a path, return an object, Parse ('/a/b/c/index.html') // const obj = path.parse('/a/b/c/') /* const obj = path.parse('./a/b/c/') console.log(obj.name) */Copy the code

1.5 the format

Serialization path

/* const obj = path.parse('./a/b/c/')
console.log(path.format(obj)) */
Copy the code

1.6 isAbsolute

Check whether the current path is an absolute path

/* console.log(path.isAbsolute('foo')) console.log(path.isAbsolute('/foo')) console.log(path.isAbsolute('///foo')) console.log(path.isAbsolute('')) console.log(path.isAbsolute('.')) console.log(path.isAbsolute('.. /bar')) */Copy the code

1.7 the join

Stitching path

/* console.log(path.join('a/b', 'c', 'index.html')) console.log(path.join('/a/b', 'c', 'index.html')) console.log(path.join('/a/b', 'c', '.. /', 'index.html')) console.log(path.join('/a/b', 'c', './', 'index.html')) console.log(path.join('/a/b', 'c', '', 'index.html')) console.log(path.join('')) */Copy the code

1.8 the normalize

Normalized path

/* console.log(path.normalize('')) console.log(path.normalize('a/b/c/d')) console.log(path.normalize('a///b/c.. /d')) console.log(path.normalize('a//\\/b/c\\/d')) console.log(path.normalize('a//\b/c\\/d')) */Copy the code

1.9 resolve

Return to absolute path

// console.log(path.resolve()) /** * resolve([from], to) */ // console.log(path.resolve('/a', '.. /b')) console.log(path.resolve('index.html'))Copy the code

Buffer

1.1 introduction

A Buffer object is used to represent a fixed-length sequence of bytes. Many Node.js apis support Buffers.

The most common use of a Buffer is as a Buffer for reading and writing files

Buffer allows JS to manipulate binary

1.2 What is a Buffer? Where is it? What to do?

The operation of files is basically the operation of binary data, which can be divided into

  • Flow operation
  • Buffer

The JAVASCRIPT language originally served the browser platform, now under the Nodejs platform JS can implement IO, how to implement IO is based on buffer and stream.

The essence of IO operations is binary data

Of course, the Stream operation is not unique to NodeJS

The flow operation and pipeline realize the segmented transmission of data. There are producers and consumers in the end-to-end transmission of data. There are always waiting in the process of production and consumption.

In Nodejs, a Buffer is a piece of memory space or Buffer

1.3 Buffer summary

  • You don’t need to require a global variable
  • Nodejs platform to achieve binary data operation
  • Does not occupy the v8 heap size memory space
  • Memory usage is controlled by Node and reclaimed by V8’s GC
  • Typically used with a stream as a data buffer

2.1 Buffer API

2.1.1 the fill

  • Fill is the same as normal JS
  • Three arguments. The second argument is the starting position of the fill
  • The third parameter is the end of the fill

2.1.2 the write

  • Three arguments (value, start, value.length)

2.1.3 toString

  • ToString // The default encoding is UTF-8
  • Three parameters (encoding format, start, end)

2.1.4 slice

  • Two parameters (start, length) note the length problem caused by different encoding formats

2.1.5 copy

  • source.copy(target, targetStart, sourceStart, sourceEnd)

2.1.6 ifBuffer

  • Check whether the current data is a buffer

    // let buf = Buffer.alloc(6)

    // Fill is the same as a normal js operation, fill // three parameters, The second argument is the start position of the fill // the third argument is the end position of the fill /* buf.fill(123) console.log(buf) console.log(buf.tostring ()) */

    // write // three parameters (value, start, value.length)

    /* buf.write(‘123’) console.log(buf) console.log(buf.toString()) */

    // toString // default encoding utF-8 // three parameters (encoding format, start, Log (buf) console.log(buf. ToString (‘utf-8’, 3, 9)) */

    // slice /* let buf = buffer. from(‘ yes toilet is ‘) let b1 = buf.slice() console.log(b1.tostring ()) */

    // copy // source.copy(target, targetStart, sourceStart, Alloc (6) let buf2 = Buffer. Alloc (6) let buf2 = Buffer. From (‘ pull ‘) buf2.copy(buf, 3, 3, 6)

    console.log(buf.toString()) console.log(buf2.toString())

2.1.6 Be familiar with API and copy split

/ / add methods on prototype ArrayBuffer. Prototype. The split = function (sep) {let len = Buffer. The from (sep). Length let ret = [] let start = 0 let offset = 0 while(offset = this.indexOf(sep, start) ! == -1) { ret.push(this.slice(start, offset)) start = offset + len } return ret }Copy the code

Fs module

1.1 FS is the built-in core module of NodeJs, which provides the API of asking price system operation

1.2 Permission bits, identifiers, and file descriptors

Permission bit

  • The user has the permission to operate the file. Generally, the creator has the permission for the file 777

identifier

  • R: can read
  • W: to write
  • S: synchronous
  • +: Perform the reverse operation
  • X: Exclusive operation

File descriptor

  • Fd is the identifier assigned by the operating system to the opened file

1.3 the fs summary

  • Fs is the built-in core module in Nodejs
  • At the code level, FS is divided into basic operation classes and common apis
  • Permission bits, identifiers, operators

1.4 File manipulation API

  • ReadFile: reads data from a specified file (path, encoding, callback)

  • WriteFile: write data to specified file (path, content, {mode: permission bit, flag: identifier, encoding})

  • AppendFile: Writes data to a specified file in the same way as writeFile

  • CopyFile: Copy data from one file to another (target, source, callback)

  • WatchFile: Monitor specified files (path, {interval: repeat call time}, (curr, prev))

    const fs = require(‘fs’) const path = require(‘path’)

    // readFile /* fs.readFile(path.resolve(‘data1.txt’), ‘utf-8’, (err, data) => { console.log(err) if (! null) { console.log(data) } }) */

    // writeFile /* fs.writeFile(‘data.txt’, ‘123’, { mode: 438, flag: ‘w+’, encoding: ‘utf-8’ }, (err) => { if (! err) { fs.readFile(‘data.txt’, ‘utf-8’, (err, data) => { console.log(data) }) } }) */

    / / / * fs appendFile. AppendFile (‘ data. TXT ‘, ‘hello node. Js, {}, (err) = > {the console. The log (‘ write success’)}) * /

    / / used by copyFile / * fs. Used by copyFile (‘ data. TXT ‘, ‘test. TXT, () = > {the console. The log (‘ copy success’)}) * /

    // watchFile fs.watchFile(‘data.txt’, {interval: 20}, (curr, prev) => { if (curr.mtime ! == prev.mtime) {console.log(‘ file changed ‘) fs.unwatchfile (‘data.txt’)}})

1.5 MD to HTML exercises

const fs = require('fs') const path = require('path') const marked = require('marked') const browserSync = Require ('browser-sync') /** * 01 03 Write the above Html characters to the specified Html file. 04 Listen for changes in the content of the MD document. */ let mdPath = path.join(__dirname, process.argv[2]) let cssPath = path.resolve('github.css') let htmlPath = mdPath.replace(path.extname(mdPath), '.html') fs.watchFile(mdPath, 0, (curr, prev) => { if (curr.mtime ! == prev.mtime) { fs.readFile(mdPath, 'utf-8', (err, HTML let htmlStr = marked(data) fs.readFile(cssPath, 'utF-8 ', (err, Replace ('{{style}}', data) => {let retHtml = temp.replace('{{content}}', htmlStr).replace('{{style}}', data) Fs.writefile (htmlPath, retHtml, (err) => {console.log(' HTML generated successfully ')})})})}) browsersync.init ({browser: ', server: __dirname, watch: true, index: path.basename(htmlPath) }) const temp = ` <! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .markdown-body { box-sizing: border-box; min-width: 200px; max-width: 1000px; margin: 0 auto; padding: 45px; } @media (max-width: 750px) { .markdown-body { padding: 15px; } } {{style}} </style> </head> <body> <div class="markdown-body"> {{content}} </div> </body> </html> `Copy the code

1.6 File Opening and Closing

  • Fs. Open (path, ‘r’ | ‘w’, (err, w | r fd) = > {}) generally divided into two, speaking, reading and writing
  • fs.close(path, callback)
  • fs.read(fd, buffer, offset, length , position)
  • fs.write(fd, buffer, offset, length ,position)

1.6.1 File Reading and Writing

  • A read operation writes data from a disk file to buffer

  • Fd Locates the currently opened file

  • Buf is used to represent the current buffer

  • Offset indicates where in the BUF the write is currently started

  • Length indicates the length of the current write

  • Position indicates where the file is currently being read from

    const fs = require(‘fs’)

    Let buf = buffer.alloc (10)

    / * *

    • Fd Locates the currently opened file
    • Buf is used to represent the current buffer
    • Offset indicates where in the BUF the write is currently started
    • Length indicates the length of the current write
    • Position indicates where the file is currently being read from

    / / fs.open(‘data.txt’, ‘r’, (err, rfd) => { console.log(rfd) fs.read(rfd, buf, 1, 4, 3, (err, readBytes, data) => { console.log(readBytes) console.log(data) console.log(data.toString()) }) }) */

    Buf = buffer. from(‘1234567890’) fs.open(‘b.txt’, ‘w’, (err, WFD) => {fs.write(WFD, buf, 2, 4, 0, (err, written, buffer) => { console.log(written, ‘—-‘) fs.close(wfd) }) })

1.6.2 Write test of large files

Const fs = require('fs') /** * 01 */ let buf = buffer. alloc(10) // 01 Open the specified file /* fs.open('a.txt', 'r', (err, RFD) => {// 03 open b file, Fs.open ('b.txt', 'w', (err, WFD) => {// 02 Reads data from an open file fs.read(RFD, buf, 0, 10, 0, (err, Fs. write(WFD, buf, 0, 10, 0, (err, Written) = > {the console. The log (' write success ')})})})}) * / / / 02 data completely copy / * fs in the open (' a.t xt ', 'r' (err, rfd) => { fs.open('b.txt', 'a+', (err, wfd) => { fs.read(rfd, buf, 0, 10, 0, (err, readBytes) => { fs.write(wfd, buf, 0, 10, 0, (err, written) => { fs.read(rfd, buf, 0, 5, 10, (err, readBytes) => { fs.write(wfd, buf, 0, 5, 10, (err, Written) => {console.log(' write successfully ')})})})}) */ const BUFFER_SIZE = buf.length let readOffset = 0 fs.open('a.txt', 'r', (err, rfd) => { fs.open('b.txt', 'w', (err, wfd) => { function next () { fs.read(rfd, buf, 0, BUFFER_SIZE, readOffset, (err, readBytes) => { if (! ReadBytes) {// If the condition is true, Fs.close (RFD, ()=> {}) fs.close(WFD, ()=> {}) console.log(' copy completed ') return} readOffset += readBytes fs.write(WFD, buf, 0, readBytes, (err, written) => { next() }) }) } next() }) })Copy the code

1.7 Directory Operation API

  • Access: Checks whether a file or directory has operation permission (path, (err))

  • Stat: Obtain directory and file information (path, (err, statObj))

  • Mkdir: create directory (path, {recursive: create recursively})

  • Rmdir: delete directory (path, {recursive: delete recursively})

  • Readdir: reads the contents of a file (path, (err, file: subdirectory under path))

  • Unlink: Deletes a specified file (path, (err))

    const fs = require(‘fs’)

    / / a, access / * fs. Access (‘ a.t xt ‘(err) = > {the if (err) {the console. The log (err)} else {the console. The log (‘ have operation permissions’)}}) * /

    Stat /* fs.stat(‘a.txt’, (err, statObj) => { console.log(statObj.size) console.log(statObj.isFile()) console.log(statObj.isDirectory()) }) */

    Three, the mkdir / * / / fs. Mkdir (a/b/c, {recursive: true}. (err) = > {if (! Err) {console.log(‘ created successfully ‘)}else{console.log(err)}}) */

    / / 4 and rmdir fs. Rmdir (‘ a ‘, {recursive: true}. (err) = > {the if (! Err) {console.log(‘ deleted successfully ‘)} else {console.log(err)}})

    Five, readdir / * / / fs. Readdir (a/b, (err, files) = > {the console. The log files ()}) * /

    Six, unlink / * / / fs. Unlink (‘ a/a.t xt ‘(err) = > {the if (! Err) {console.log(‘ deleted successfully ‘)}}) */

1.7.1 Creating Directory Synchronization

Const fs = require('fs') // 一, access /* fs.access('a. (err) = > {the if (err) {the console. The log (err)} else {the console. The log (' have operation permissions')}}) * / / / 2, stat / * fs stat (' a.t xt '(err, statObj) => { console.log(statObj.size) console.log(statObj.isFile()) console.log(statObj.isDirectory()) }) */ // Three, the mkdir / * fs. Mkdir (a/b/c, {recursive: true}. (err) = > {if (! Rmdir fs.rmdir('a', {recursive: true}, (err) => {if (! Err) {console.log(' deleted successfully ')} else {console.log(err)}}) readdir /* fs.readdir('a/b', (err, Files) = > {the console. The log (files)}) six, unlink / * * / / / fs. Unlink (' a/a.t xt '(err) = > {the if (! Err) {console.log(' deleted successfully ')}}) */Copy the code