Home > Web Front-end > JS Tutorial > Basic learning of node: What you need to know about the front-end [Summary]

Basic learning of node: What you need to know about the front-end [Summary]

青灯夜游
Release: 2023-03-30 20:51:36
forward
1984 people have browsed it

This article will talk about the basic knowledge points of node, and summarize and share some nodejs knowledge that the front-end needs to know. I hope it will be helpful to everyone!

Basic learning of node: What you need to know about the front-end [Summary]

Whether it is front-end development or back-end development, I personally believe that you should not be limited to your own field. Only by breaking through the comfort zone can you improve. Although it can make perfect, but We also need to know that learning without thinking will lead to confusion, so it is necessary to understand the server-side knowledge that is most closely related to the front-end. The emergence of nodejs can be said to give front-end developers a faster way to understand the backend. The following are some of my experiences in learning nodejs from the front-end perspective.

Basic features

To quickly understand nodejs, you can look at the following aspects: node’s module concept (ECMAScript2015 has supported the front-end and is easy to understand), V8 engine ( Same as browser), asynchronous operation (based on v8 and slightly different from browser processing), event-driven (based on v8 and slightly different from browser), node basic API. [Related tutorial recommendations: nodejs video tutorial, Programming teaching]

module

##nodejs The modules can be roughly divided into three types: core modules, third-party modules, and custom modules. Each module has a different loading priority.

    Core module: nodejs built-in module, which can be understood as the basic API of nodejs, such as our commonly used path os fs and so on. These modules are also the basis for our nodejs to interact with the server.
  • Third-party module: npm package installed by the nodejs package management tool platform.
  • Custom module: This usually represents our own defined file module.

Module loading and compilation

    File parsing path: Check if cache exists=》Check if it is a core module=》Check extension=》 Parsing execution (according to different suffix names)
  • //检查fs内存中的缓存是否存在如果不存在则加载fs模块
    let fs = require("fs")
    //检查fs内存中的缓存(无)=》检查是否核心模块=》检查扩展名
    let demo = require("./demo")
    Copy after login
    Cache priority principle: From the file parsing path, we can see that nodejs will first check whether the cache in the memory exists, and if it exists, load the cache.
  • Module compilation: This article will not explain this process. The next chapter will explain this process in detail.

Standards

The basic concept of JavaScript module development is as follows: "script" introduction => scope function => self-executing function => Commonjs (AMD ). Modules in nodejs mainly adopt commonjs specifications, as shown below.

    Each file is a module and has its own scope.
  • The variables, functions, and classes defined in each file are private and invisible to other files
  • Each module can be accessed through
  • exports or module.exports Externally exposed interface
  • Each module loads another module through
  • require
We commonly use require exports in nodejs module.exports It is based on Commonjs.

Asynchronous operations

Asynchronous operations are easy to understand for front-end development. We are full of asynchronous operations, callback functions, and promises in JavaScript. , setTimeout, these are all asynchronous related operations. The most basic DOM rendering is also asynchronous. This is the most closely related area between nodejs and JavaScript. However, please note that there are some differences between these two processing methods. The differences will not be discussed here. Will be updated later. The following is a brief description of the asynchronous characteristics of node:

    Asynchronous is realized through Event Loop, which includes the concepts of macro tasks and micro tasks
  • Node and JavaScript have some differences in asynchronous processing Difference
A picture of the event loop found on the Internet:

Basic learning of node: What you need to know about the front-end [Summary]

V8 engine

    V8 is the name of the JavaScript engine that drives Google Chrome. This is something that takes our JavaScript and executes it while browsing with Chrome.
  • In Nodejs, v8 is used. It provides a variety of callable
  • API, such as reading and writing files, network requests, system information, etc. On the other hand, because CPU executes machine code, it is also responsible for interpreting JavaScript code into a sequence of machine instructions for execution. This part of the work is performed by the V8 engine Complete

The core of nodejs is V8, v8 is used to compile JavaScript into a language that can be recognized by the machineBasic learning of node: What you need to know about the front-end [Summary]

Event-driven

Event-driven is actually a commonly used architectural pattern in software architecture. Simply put, it creates (registers) an event and listens to the event, and processes it according to the status of the event. Most of the core APIs in nodejs are built around the idiomatic asynchronous event-driven architecture. In addition, the core module events in node can be used to create custom events.

Common API

  • fs: Commonly used for file viewing, editing, creation and other operations
  • http: The key to the network Module
  • socket: socket network communication
  • events: event module

Application scenario

  • Background service
  • Script processing

Server side

In the first few years when nodejs appeared, there was a question about whether to use nodejs In a production environment, most developers are not optimistic about it. However, after practice in recent years, nodejs's single-threaded application in handling high-concurrency scenarios has been well tested. There are currently many nodejs-based services in online products. In addition, nodejs also has many stable server-side frameworks similar to java spring. Here are some commonly used ones

  • Koa: Onion model development model
  • Express: Routing as the core Server-side nodejs framework
  • Fastify: A framework that takes up very little resources and is extremely fast. Currently, it is the fastest framework.

Note: There are many frameworks for different business types. If you are interested, you can learn about them

Tools

When we use vue or react family bucket for development, have you ever thought about why a single line of commands can run the front-end service? Why can one line of commands compile the front-end? Why did our Vue code end up outputting a bunch of js? When we study the source code of these functions, we will find that almost all of these capabilities are developed based on nodejs. The following lists our use of nodejs in daily tools.

  • Local service: The local service plug-ins enabled by webpack are all implemented based on the http module of nodejs
  • Compilation and packaging: vue's .vue file, react's jsx or commonly used .ts files How did it finally become a js file? Everyone thinks it was webpack. In fact, the entire webpack is based on nodejs. The ability to compile files is closely related to the fs module of nodejs.

Summary

I roughly talked about the basics of nodejs from the perspective of the front end. Each point can be taken out individually to explain a lot. Here is just to let everyone know. Have a general understanding of node to facilitate subsequent learning. At present, I have completed many projects using nodejs. After using it more, I can look at some front-end technical developments from a broader perspective.

For more node-related knowledge, please visit: nodejs tutorial!

The above is the detailed content of Basic learning of node: What you need to know about the front-end [Summary]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template