1. Advantages and disadvantages of Node

    1. The advantages

    1. Asynchronous non-blocking I/O(file read and write, database read and write)
    2. Event loop mechanism
    3. Single thread
    4. Cross-platform (written in one place, used in many places) 2. Disadvantages
    5. Too many nested callbacks (callback hell)
    6. Single-threaded, poor processing of CPU-intensive tasks
  2. Application scenarios

    1) Web server API

    2) The server renders the page to improve the speed

    3) Back-end Web services, such as cross-domain, server-side requests

  3. Compare with a traditional server

    1) Java server multithreading, Node single thread

    2) Node server single thread control all processes, once the error all will be wrong

    3) Node is I/O intensive (with clear requirements) and has the advantage of getting data from the database

    4) Insufficient Node: not SUITABLE for CPU intensive (request unclear)

    5) JaV server is preferred regardless of cost

  4. Node function Features

    1) Every module (js file) in node is wrapped in an outer function

    Function (exports, require, module, __filename, __dirname){}

    3)__filename: indicates the absolute path of the current file

    4) __dirName: the absolute path of the current file folder

    5) The function of the outer layer :① hide the internal implementation (function outside the invisible)

    Support for CommonJS modules (exports, require, module cannot be imported)Copy the code
  5. The node of

    1)js:dom+bom+ecmascript

    2) Node :es most specification +global

  6. Packages and Package Manager (NPM)

    1) Package: a folder on the computer contains certain files that conform to certain structures. It is a package

    2) What does the package contain?

    ① Package. json– Describe the file

    ②bin– Executable binary file

    ③ Lib — compiled JS code

    (4) doc – document

    ⑤ Test — Test report

    NPM I XXX -S = NPM I XXX –save = NPM I XXX -> Dependencies

    ② NPM I XXX -d = NPM I XXX –save-dev -> Devdependencies

    ③ NPM I XXX-G global installation

    ④ NPM I XXx@ Version Install the package of the specific version

    ⑤ NPM I installs all packages of package.json

    ⑥ NPM remove XXX

    4)^ Lock the large version to lock the small version

    5) Set taobao mirror: NPM config set Registry registry.npm.taobao.org

  7. cnpm

  8. yarn

  9. Installation package: YARN Add Package name (partial installation) Global installation: YARN Global add package name

    2) Download all package yarn declared in package.json

    3) YARN caches downloaded packages and can be installed even without a network

    4) We don’t have our own warehouse. We use NPM’s warehouse

    5) Installation: NPM I yarn-g

    6) Set yarn config set Registry registry.npm.taobao.org

  10. The Buffer Buffer

    1) Is an array-like object used to store data (binary data)

    2)Buffer is highly efficient, fast in storage and reading, and can operate directly on the computer’s memory

    3) Once the Buffer size is determined, it cannot be modified

    4) Each element takes up 1 byte of memory

    5)Buffer can be used directly without introduction

    6) Create Buffer instance with new (soon to be deprecated)

    Cache.from () buffer.alloc () buffer.allocunsafe ()

  11. Ordinary file write

    1)require(‘fs’)

    2)fs.writeFile(file, data, options, callback)

    3) File: file path + file name

    4)options:flag, mode, encoding

    Flag: default 'w', optional 'w(overwrite)', 'a(append)' mode:0o111(file executed) 0O222 (file can be written) 0o444(read-only)Copy the code
  12. Streaming write file

    1)var b = fs.createWriteStream(path,options)

    2)b.write()

    3)b.on()

    4)b.end()/b.close()

  13. Ordinary file reading

    1)fs.readFile()

  14. Streaming file reading

    1)fs.createReadStream(path,options)

    2) Use streams to be monitored

  15. The database

    1) Relational database (MySql,Oracle)

    ① Use a table structure

    SQL language universal, can be used for complex queries

    ③ Poor reading and writing ability, low flexibility (fixed table structure)

    1) MongoDB,Redis…

    2) Characteristics: The relationship is not close. There are documents, there are key-value pairs

    3) Comparison: table — Set (non)

    Every piece of data -- Every document (non)Copy the code

    4) Advantages: ① The format is flexible and the form of storing data is key and value

    ② Fast ③ Easy to use: easy to deployCopy the code

    5) Disadvantages: ① Does not support SQL

    Transactions: atomic (indivisible), is a complete set of processes, that is, either success or failureCopy the code
  16. MongoDB Basic commands

    1. Db: Checks which database is being operated
    2. Show DBS: Check the database list (there are several databases, note: if the database is empty, it does not appear in the list)
    3. Use test: Switch to the test database, or create a test library if it does not exist
    4. Db.students.insert ({}) : Inserts a document into the STUDENTS collection of the current database.
    5. Show Collections: Shows all collections in the current database.

    Sc. exe create MongoDB binPath=””C:Program Files\MongoDB\Server\4.2\bin\mongod. Exe “–service –config=”C:Program Files \ mongo \ Server \ 4.2 \ mongod CFG “DisplayName =” mongo “start =” auto”

  17. The node server

    1)querystring — querystring