Nodejs knowledge that the front end needs to know (basic)

runaway pikachu 2022-08-06 08:58:32 阅读数:505

nodejsknowledgeendneedsknow

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情

前言

Whether it is front-end development or back-end development, I personally think that it should not be limited to its own field,Go beyond your comfort zone to improve,Which one can make a coincidence,But we also need to know that learning without thinking leads to loss,Therefore, it is necessary to understand the server-side knowledge that is most closely related to the front-end.,而nodejsThere is made the front-end development to understand the background to have a more efficient path.Here is his former side Angle to studynodejs的一些经验.

基本特点

快速了解nodejs可以从以下几个方面来看看:node的模块概念(ECMAScript2015Supported front end is easy to understand),V8引擎(same as browser),异步操作(基于v8Slightly different from browser handling),事件驱动(基于v8slightly different from browsers),node基础API

模块

nodejsThe modules can be roughly divided into three types:核心模块 第三方模块 自定义模块.The loading priority of each module is different.

  • 核心模块:nodejs内置模块,可以理解为nodejs的基础API,例如我们常用的path os fs等等.These modules are also ournodejsThe basics of interacting with the server.
  • 第三方模块:nodejsPackage management tool platform installednpm包.
  • 自定义模块:This usually value is our own defined file module.

Loading and compiling of modules

  • 文件解析路径:Check if there is a cache =》Check if it is a core module=》检查扩展名=》解析执行(According to different suffix names)
//检查fsDoes the in-memory cache exist and loads if notfs模块
let fs = require("fs")
//检查fs内存中的缓存(无)=》Check if core module=》检查扩展名
let demo = require("./demo")
复制代码
  • 缓存优先原则:We can see from the file parsing pathnodejsWill first check whether the cache in memory exists,load cache if exists
  • 模块编译:

规范

JavaScriptThe concept of module development is basically as follows: "script"引入=> 作用域函数=》自执行函数=〉 Commonjs(AMD).在nodejsThe modules in the main usecommonjs规范,如下所示.

  • 每个文件就是一个模块, 有自己的作用域.
  • variables defined in each file、函数、类都是私有的,对其它文件不可见
  • Inside each module can pass exports 或者 module.exports 对外暴露接口
  • 每个模块通过 require load additional modules

我们在nodejs中常用require exports module.exports都是基于Commonjs来的.

异步操作

Asynchronous operations are easy to understand for front-end development,我们在JavaScriptAsynchronous operations are everywhere in,回调函数,promise,setTimeoutThese are asynchronous related operations,最基础的DOMRendering is also asynchronous,nodejs和JavaScriptThis is the one that is most closely related,But everyone please pay attention to the two way has some different,The difference is not mentioned here, and it will be updated later.Briefly described belownode的异步特点:

  • 异步是通过Event Loop实现,Among them are the concepts of macrotasks and microtasks
  • node和JavaScriptThere is a difference in asynchronous processing

A diagram of the event loop found on the Internet: image.png

V8引擎

  • V8 是驱动 Google Chrome 的 JavaScript 引擎的名称. 这是在使用 Chrome 浏览时获取我们的 JavaScript 并执行它的东西.
  • 在Nodejs,v8is used which provides a variety of callablesAPI,如读写文件、网络请求、系统信息等.另一方面,因为CPU执行的是机器码,它还负责将JavaScript代码解释成机器指令序列执行,这部分工作是由V8引擎完成

image.png nodejs的内核是V8,v8是用来将JavaScriptCompiled into a machine-readable language

事件驱动

Event-driven is actually a common architectural pattern in software architecture.,This is by creating a simple(注册)event and listen for this event,Process according to the state of the event.nodejsmost of the core API 都是围绕惯用的异步事件驱动架构构建的,另外node中的核心模块eventsCan be used to custom create events.

常用API

  • fs:Commonly used for file viewing,编辑,创建等操作
  • http:网络的关键模块
  • socket:socket网络通信
  • events:事件模块

应用场景

  • 后台服务
  • 脚本处理

服务端

在nodejsthe first few years,对于是否将nodejs应用在生产环境中,Most developers are not optimistic.But after practice in recent years,nodejsThe application of single-threaded processing of high concurrency scenarios has been well tested.Has quite a few based on the current online productsnodejs的服务.另外nodejsThere are also many similarjava springStable server-side framework,Here are some commonly used

  • Koa:Development mode based on the onion model
  • Express:Routing-centric servernodejs框架
  • Fastify:A very small footprint,and extremely fast framework,目前来看,It is the fastest framework.

注:There are many frameworks for different business types,大家有兴趣的可以去了解下

工具

我们在使用vue或reactWhen the whole family barrel is developing,You have to think about why a line command can run up the front end of service?Why is one line of command able to compile the front-end??why we thankvueCode final output a heapjs?When we study the source code of these functions, we will find that almost all of these capabilities are based onnodejs来开发.We in the daily tools listed belownodejs的使用.

  • 本地服务:webpackThe enabled local service plugins are based onnodejs的http模块实现的
  • 编译打包:vue的.vue文件react的jsx或者常用的.tshow the file ends up beingjs文件的,Everyone think it iswebpack其实整个webpack都是基于nodejswhere the compile file capability is andnodejs的fsModule is linked to the.

总结

From the front end point of viewnodejs的基础,Each point can be taken out individually to say a lot,Here is just for everyone tonode有个大概的了解,方便后续的学习.Personally also usenodejsCompleted many projects,After using more, I can see some technical developments in the front-end from a broader perspective.

版权声明:本文为[runaway pikachu]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/218/202208060848354288.html