1. What is Node.js?

  • Node.js is based onChrome V8 engineJavaScript runtime environment.
  • Node.js uses oneEvent-driven, non-blocking I/OThe model makes it lightweight and efficient.

2. Is running JavaScript in Node.js the same as running JavaScript in Chrome?

  • Similarities:
    • Chrome uses the same JavaScript engine and model.
  • Difference:
    • Node.js has no browser API, i.e. Document, window, etc.
    • Added a lot of Node.js apis.
    • For developers, Node.js:
      • You write JavaScript in Chrome to control the browser.
      • Node.js lets you control your entire computer in a similar way.

3. What can Node.js be used for?

  • The Web service
    • Search engine optimization + first screen speed optimization = server rendering
    • Server rendering + front and back isomorphism = Node.js
  • Build workflow
    • webpack
  • The development tools
    • Visual Studio Code
  • The game
  • scalability
    • Large applications need to give users the ability to customize modules
    • Use Node.js for complex native applications
      • You can leverage the flexibility of JS to provide external extensions
      • JS’s large developer base allows them to take advantage of their flexibility
  • Client application
    • New client applications need to be developed in the case of existing websites.
    • With Node.js client technology (ELECTRON) to achieve maximum reuse of existing projects.
  • Backend for Frontend
    • Provides HTTP services on the user side
    • Use the back-end RPC service

4. Node.js adopts CommonJS module specification

5. NPM is a package management tool for Node.js

Packages are Node.js modules written by someone else

6. Node.js built-in module

Official website: nodejs.org/dist/latest…

7. Non-blocking I/O of Node.js

  • I/O is Input/Output, the Input and Output of a system.
  • This is the difference between blocking and non-blocking I/OWhether the system can receive other inputs between input and output.

8. Node.js asynchronous programming

  • callback
    • Callback function format specification
      • error-first callback
      • node-style callback
    • The first argument is error, and the following arguments are the results.
  • Promise
    • The current event cycle doesn’t give you results, but future events will give you results
    • It’s a state machine
      • pending
      • fulfilled / resolved
      • rejected
    • . And then. The catch
      • Resolved promises call back to the first.then
      • A Promise in the Rejected state calls back to the first.catch
      • Any Promise with the Rejected state and no. Catch following it causes a global error in the browser/Node environment
    • Executing then and catch returns a new Promise, the final state of which is determined by the result of executing the callbacks to then and catch
      • If the callback function ends up as a throw, the Promise is in the Rejected state
      • If the callback function ends up being return, the Promise is resolved
      • However, if the callback function eventually returns a Promise, the Promise state is the same as the Promise state of the callback function return
  • async/await
    • Async Function is a syntactic sugar wrapper for promises
    • The ultimate in asynchronous programming – write asynchronously in a synchronous manner
      • The await keyword can “suspend” the execution of async function
      • The await keyword gets the execution result of a Promise in synchronous writing
      • A try-catch can get an error from an await

9. Express and Koa

  • Express core features:
    • routing
    • Request/response is simple
      • Request: PathName, query, etc
      • Response: send(), json(), jsonp(), etc
    • The middleware
      • Better organization of process code
      • Asynchrony breaks the Onion model of Express
  • Koa Core functions:
    • Extreme request/ Response simplification over Express
      • ctx.status = 200
      • ctx.body = ‘hello world’
    • Middleware implemented using async Function
      • The ability to “suspend execution”
      • It also fits the Onion model in the asynchronous case
    • The kernel was streamlined and all additional functionality was moved to middleware.
  • Express vs Koa
    • Express has lower barriers, Koa is more powerful and elegant.
    • Express packages more, develops faster, and Koa customizable is higher.
  • Which is “good” and which is “bad”?
    • There is no superior or inferior framework
    • Different frameworks have different application scenarios

10. RPC calls

Definition: Remote Procedure Call

  • What are the similarities with Ajax?
    • Network communication between two computers
    • Both parties need to agree on a data format
  • What’s different about Ajax?
    • DNS is not necessarily used as the addressing service
    • Generally, HTTP is not used for application layer protocols
    • Based on TCP or UDP
    • Addressing/load balancing
      • Ajax: Address using DNS
      • RPC: Addressing using a unique service
    • TCP communication mode
      • Simplex communication
      • Half duplex communication
      • Full duplex communication
    • Binary protocol
      • Smaller packet size
      • Faster codec rate

11. Node.js Buffer codec binary data packets

  • Size end problem
    • In several bytes, high and low values are arranged in different order.
  • The method is similar to string
    • Use concat instead of + to avoid UTF-8 character concatenation problems.
  • Protocol Buffer
    • Binary protocol codec library developed by Google
    • The format of Buffer is controlled by the protocol file
      • More intuitive
      • Better to maintain
      • Easier to work with

Node.js NET builds multiplexed RPC channel

  • Simplex/half duplex communication channel setup
  • The full-duplex communication channel is set up
    • The key is that application layer protocols require fields that mark the packet number
    • Fields that mark the length of the package are required to handle the following cases
      • Stick package
      • Not complete package
    • Error handling