This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact

A,Node.jsintroduce

Making the warehouse:Node.js

1.1 introduction

Node.js philosophy: spend minimum hardware cost, pursue higher concurrency, higher processing performance

In Chrome, the V8 engine used for JavaScript parsing was moved to the server by Ryan Dahl to make the server software called Node.js.

The author submitted the first line of code, named Node, in February 2009, and the project was officially announced in May. Node.js became popular at the end of 2009 after a talk at JSConf EU in Berlin.

The role of 1.2:

Resolve server performance bottlenecks

  • Access to local files: read/modify/delete etc
  • Build server
  • Connecting to a Database
  • Play with multiple processes

Node.js is a zhuan project focused on high performance Web server optimization. It was born out of frustration, exploration, and V8.

A development platform that uses JavaScript for programming and allows JavaScript to run on the server side, on par with PHP/JSP/Python/Ruby etc. (but these four are both platforms and languages)

Because Node.js is a single thread, it will be blocked when computing services are heavy, and other users will not be able to access it. Therefore, Node.js is suitable for the development of I/O businesses, but not for the development of computing services. In addition, node.js’s extreme mode also makes it difficult to be competent for projects with high security and large volume.

Node.js is widely used and has the world’s largest open source library, NPM, and features like Python for top-level routing design.

Node.js is a runtime environment in which you can run JS code. Node.js makes JavaScript more flexible and not limited to the client.

1.3 Node.jsThree characteristics of:

  1. Event-driven – based on events and callback functions
  2. Single thread – reduces memory overhead
  3. Asynchronous I/O (non-blocking I/O)

Actually, the three characteristics are the same thing

Weaknesses:

  1. Unable to utilize multi-core CPUS
  2. An error can cause an entire application to quit, and the robustness of the application is worth testing
  3. A large amount of computation consumes the CPU, causing asynchronous I/O calls to fail.

1.4 Node.js event loopEvent Loop

Event loops are the mechanism by which Node.js handles non-blocking I/O operations

Methods are passed as arguments and then executed synchronously, often called callback, on a single thread. This is going to be a single thread again.

When Node.js starts, it initialises the event loop, processes the supplied input script (or dumps it into the REPL, which is not covered in this article), and it may call some asynchronous API, schedule the timer, or call process.Nexttick () to start processing the event loop.

See the official Nodejs.org documentation – Event Loops for more details

1.5 the middleware

MiddleWare can be thought of as a tool (function) that filters and preprocesses user requests,

In Node.js, middleware mainly refers to method functions that encapsulate the details of HTTP request handling. It typically does not respond directly to the client, but rather passes on the results of the processing.

2, Node.js test knife use

2.1 installation

Official website: nodejs.org/en/ / Official website: nodejs.org/zh-cn/

Download the installation package:

Download the installation package and install it

After the installation, run the node -v command to view the node version — v14.17.5

2.2 the use of the Node. Js

  1. The command line

REPL tools: (CMD –> node)

Enter node enter to enter the Node command line environment, you can perform JavaScript operations

Tips: You can open a separate terminal to easily verify that the Node API and JavaScript API are correct

Node. Js NPM instructions

NPM: Node Package Manager node.js is a Package management tool.

NPM helps us publish, install, and rely on third-party modules. With NPM, Node.js and third-party modules form a huge ecosystem of Node.js.

Node.js development tool installed with node.js, easy to install, upload, download package management tools.

Node.js file system

The node.js file system, in short, uses the node.js built-in module FS to manipulate files in the local system

All operations in the FS module have a choice of two forms: synchronous and asynchronous

Here’s a quick list of some apis

Synchronous file write, operation steps:

  1. Open the file
fs.openSync(path,flags [, mode])
Copy the code
  1. Writes to a file
fs.writeSync(fd,string [ , position [, encoding]])
Copy the code
  1. Save and close the file
fs.closeSync(fd)
Copy the code

Write to asynchronous files

  1. Open the file
fs.open(path,flags[, mode],callback)
Copy the code
  1. Write to a file asynchronously
fs.write(fd,string[ ,positon[ , encoding] ],callback)
Copy the code
  1. Close a file
fs.close(fd, callback)
Copy the code

Simple file writing:

fs.writeFile(file,data[,options],callback)
fs.writeFileSync(file,data[, options])
Copy the code