当前位置:网站首页>Notes to nodejs (IV)
Notes to nodejs (IV)
2022-06-23 22:49:00 【Qiu Qiu Qiu YF】
I have been studying books recently Explain profound theories in simple language nodejs , Some notes written casually , Share with you ~ If there is a mistake , Welcome to correct ~
structure Web application
Node Appearance , Break down the front and rear barriers , With event driven and V8 A high performance , Become a leader in the service side . Next, we will expand the description Web Details and principles of application in backend implementation
Basic function
- The judgment of the request method : The common one is GET and POST, Besides, there is HEAD,DELETE,PUT etc. ( stay RESTful class Web In service , The request method is very important , Determines the operation behavior of resources )
- URL Path resolution : The most common application that performs business processing according to the path is the static file server , It will find the files in the disk according to the path , Then it responds to the client . In addition, the controller is selected according to the path , Its default path is a combination of controller and behavior , No additional configuration of routing information is required .
- Query string parsing The query string is after the path , This part often needs to be used by business logic . Before the business call is generated , Our middleware or framework will convert query strings , Then it is mounted on the request object for business use .
- Cookie Parsing HTTP Is a stateless protocol , Real business needs a certain state , Otherwise, the user identity cannot be distinguished .Cookie It is the earliest way to identify and authenticate a user . Cookie The process of is divided into three steps :
- The server sends... To the client Cookie
- The browser will Cookie preservation
- After that, every browser will Cookie To the server side
Cookie Some of the settings for :
- Path: Express this Cookie The path of impact , When the access path does not satisfy the match , The browser will not send this Cookie
- Expires and Max-age Is used to tell the browser this Cookie When did it expire .Expires Tell me when it expires ( Point in time ),Max-age Tell me how long it will expire ( Period of time )
- HttpOnly: Tell the browser that you are not allowed to go through document.Cookie To change it ( In fact, after setting ,js In this Cookie Directly invisible )
- Secure: true It's in HTTP Invalid in ,HTTPS In order to be effective .
Cookie Performance impact : because Cookie Implementation mechanism , Once the server sends the settings to the client Cookie The intent of the , Unless Cookie Be overdue , Otherwise, the client will send these every time it sends a request Cookie To the server side , Once set Cookie Too much , This will cause the message header to be larger . Most of the Cookie You don't need to use it every time , Because this will cause partial waste of bandwidth Better not put Cookie Set under the root node , Otherwise, almost all requests under the sub path will bring this Cookie. Use different domain names for static components ( Static files care little about state ,Cookie It is almost useless for it )( But it may increase DNS Inquire about )
- Session adopt Cookie You can record the status . however Cookie Not perfect , As mentioned above , Oversize is a significant problem , It can also be modified by the front and back end .Cookie The protection of sensitive data is invalid . To solve this problem ,Session emerge as the times require .Session The data is only kept on the server side , The client cannot modify , In this way, the security of data is guaranteed , Data does not need to be transferred every time in the protocol . Generally based on Cookie To map users and data
- cache The traditional client only needs to transfer data in the process of using after installation ,Web The application also needs to transport the components that make up the page (HTML, CSS, JS Documents, etc. ) This part of the content does not change frequently in most scenarios , But it needs to be passed in every application , If not handled , Then it will cause unnecessary bandwidth waste . If the network speed is poor , It takes more time to open the page , It also has an impact on the user experience . Therefore, saving unnecessary transmission is beneficial to both users and servers . How to make browsers cache our static resources , This is also iyge Things that need to be done jointly by the server and the browser . Generally speaking ,POST、DELETE、PUT Such behavioral request operations do not do any caching , Most caches only apply to GET In request . simply , When there is no local file , The browser must request the contents of the server , And put this part in a local cache directory . On the second request , He will check the local documents , If you are not sure whether this local file can be used directly , He will launch a Condition request . The so-called conditional request , It's in the ordinary GET In the request message , Incidental If-Modified-Since Field . It will ask the server if there is a version that needs to be updated , Last modification time of local file . If the server does not have a new version, it only needs to respond to one 304 The status code , The client uses the local version , Otherwise, return to the new version , The client will discard the local version . Conditional requests are time stamped , But there are some defects : Time , If you change the contents of a file, you don't have to change it , And frequently updated content may not be effective . To solve this problem , Introduced ETag(Entity Tag). ETag Generated by the server , The server can determine its generation rules , Generally, hash values are generated according to the contents of the file (If-None-Match/ETag). But the above two methods will still initiate a HTTP request , The client will still spend some time waiting for the response . It can be seen that the best way is not to initiate a conditional request . You can set Expires or Cache-Control( It can avoid problems caused by inconsistent time between the browser and the server ) head .Max-age Will be covered Expires
Data upload
The contents of the header message can meet most business logic operations , But a simple header message cannot carry a large amount of data , In the business , We often need to accept some data , For example, form submission , File submission ,JSON Upload ,XML Upload, etc .
Node Medium http The module is only for HTTP The header of the message is parsed , Then the trigger request event . If part of the request is included ( Such as POST With message and content ), The content part needs to be received and parsed by the user . Through the header Transfer-Encoding or Content-Length You can determine whether the request contains content . stay HTTP_Parser After the parsing message header is accepted , The message content will pass data Events trigger , We just need to process it as a stream .( Will receive buffer The list is converted to a Buffer After the object , And then convert it to a string without garbled code , Mounted on req On )
The form data ,Content-type by application/x-www-urlencoded, The content of the report style is the same as that of the query string
JSON file , application/json( Reuse JSON.parse analysis )
xml file ,applicaton/xml ( Parsing with third party Library )
Attachment upload , multipart/form-data, One more boundary Delimiter .
Path resolution
- File path
- Static files ( common )
- Dynamic files ( Relatively rare )
- MVC The main idea is to separate business logic according to responsibilities , It is divided into Controller( A collection of actions ), Model( Data related operations and encapsulation ), View( Rendering of views ) Working mode : Route resolution , according to URL Find the corresponding controller and behavior . Behavior calls related models , Data manipulation . After data operation , Call the view and related data to render the page , Output to client .
- RESTful Reprensentational State Transfer Presentation layer state transition . It mainly regards the content provided by the server as a resource , And embodied in HTTP On the way .
middleware
Introducing middleware to simplify and isolate infrastructure ( For example, query string parsing ,cookie And so on ) And business logic . Let developers focus on business development , To achieve the purpose of improving development efficiency . The behavior of middleware is similar to that of filter , Before entering the specific business logic , Let the filter handle . Each middleware handles relatively simple logic , Finally, a powerful basic framework .
The context of middleware is also request object and response object :req and res. because Node The reason for asynchrony , We need to provide a mechanism , Notify the next middleware to execute after the current middleware processing is completed . The tail trigger method is adopted to realize .
const middleware = function(req, res, next) {
//TODO
next()
}Middleware exception handling : Because the exception of asynchronous method cannot be caught directly , Exceptions generated asynchronously by middleware need to be thrown out by themselves ( next(err) The way ). Middleware for exception handling can be added , Add one more parameter (err, req, res, next)
Middleware and performance :
- Write efficient middleware : Improve the processing speed of a single processing unit , Call as early as possible next.( Cache the results of repeated calculations , Avoid unnecessary calculations, etc )
- Rational use of routing ( for example : File path type static file query , add /public Prefix , lest / All the requests of are through the middleware )
Page rendering
- Content response : utilize Content-* Field of .(Content-Type Of MIME It is very important ,Content-Disposiition It can be used to set whether the data is downloaded as an attachment even if the content is browsed )
- View rendering : It's usually a template + Data rendering
- Templates : In fact, it is string splicing ( Grammatical decomposition , Put forward ordinary strings and expressions , Processing expressions , Generate the statement to be executed , Execute with data , Generate the final string )( Generally used new Function, with Equal grammar , Not recommended ) Finally, in order to avoid xss Loophole , Must be able to form HTML String escape of tag . in addition , It's easy to understand Bigpipe
Play with the process
Node The selection V8, That means his model is similar to the browser .JS Running on a single thread of a single process , The advantage is : The program status is single , There are no locks without threads 、 Thread synchronization problem , The operating system also has less context switching during scheduling , Can improve CPU The usage rate of . But single process and single thread is not a perfect structure , Now CPU Basically multi-core , One Node The process can only use one core , This will throw out Node The first problem of practical application : How to dispose and utilize multi-core CPU The server . in addition , because Node Execute in single line length , Once the exception is not caught , It will cause the whole process to collapse . This brings up the second question : How to ensure the robustness and stability of the process .( Strictly speaking ,Node Not really a single threaded architecture , There is a certain I/O Threads exist )
Change of service model
Stone Age : The execution model is synchronized , Serve only one request at a time , All requests wait for service in order . Assume that each response service consumes N second ,QPS(query per second) by 1/N.
Bronze Age : Replication process . In order to solve the concurrency problem of synchronous Architecture , The simple improvement is to serve more requests and users at the same time through process replication . Each request requires a process to service , It's expensive , There will be many copies of the same state , More wasteful . Assume that the upper limit of the process is M,QPS by M/N
The silver age : Multithreading . In order to solve the waste problem of multiple processes , Multithreading was introduced , Let a thread serve a request . Threads are much less expensive than processes , And data can be shared between threads . But each thread of multithreading still needs its own independent stack , So it is only slightly better than multi process . And because of one CPU The core can only do one thing at a time , The operating system can only CPU section , Let threads use resources more evenly , But switching threads also requires switching thread context . So when there is a lot of concurrency , Still can not achieve strong scalability . Assume that the thread resource is occupied by the process 1/L,QPS by M*L/N
The golden age : Event driven .Apache Is the use of multithreading / Multi process model , When the concurrent number increases to tens of thousands , The problem of memory consumption is exposed . To solve the problem of high concurrency , Event driven models have emerged , image Node and Nginx All of them are based on the event driven approach , Using single thread avoids unnecessary memory overhead and context switching . The problem is :CPU Utilization and robustness .
Multi process architecture
Facing single process and single thread for multi-core CPU The problem of underutilization , Just start multiple processes . Ideally, each process would use a CPU, To achieve multi-core CPU The use of ,Node Provides child_process modular , And it provides child_process.fork() For us to replicate the process .
Master-Worker Pattern . There are two kinds of processes : Main process and working process . The main process does not serve specific business processing , Instead, it is responsible for scheduling or managing work processes . The work process is responsible for the specific business processing . however fork The process is still very expensive . We're just trying to make the most of multicore CPU, Not to solve concurrency problems .
Interprocess communication
stay Master-Worker In the pattern , To achieve the main process management and scheduling work process functions , Communication between the main process and the worker process is required . Between threads through send Method send data ,message Event to listen to the received data in real time . adopt fork After creating the subprocess , In order to achieve communication between the parent and child processes , Between the parent process and the child process IPC(Inter-Process Communication) passageway , Between the parent and child processes message and send The message .
The parent process before actually creating the child process , Will create IPC Channel and monitor it , Then the child process is really created , And tell the child process this through the environment variable IPC The file descriptor of the channel . The subprocess is in the process of starting , Connect the existing file according to the file descriptor IPC passageway , So as to complete the connection between parent-child processes . It is a two-way communication , The interprocess communication is completed in the system kernel , There is no need to go through the actual network layer .( Only the child processes started are Node process , Only child processes can connect IPC passageway )
Handle passing : A handle is a reference that can be used to represent a resource , It contains the file descriptor pointing to the object . For example, a handle can be used to represent a server socket object , One client socket object , One UDP Socket , A pipe, etc . Sending a handle means , Received in the main process socket After the request , Put this socket Send directly to the work process ( Or directly put a TCP The server sends it to the child process )( This does not mean that any object can be sent , Just messaging , Not really passing objects , The underlying details involved will not be repeated ).
Ports listen together : Relying on handle passing, multiple processes can listen to the same port without causing exceptions .( about sned For the service restored from the sent handle , Their file descriptors are the same , Therefore, no error will be reported when listening to the same port , But file descriptors can only be used by one process at a time , These process services are preemptive )
Load balancing :
Listen for the same port between multiple processes , Enables user requests to be distributed to multiple processes for processing , The advantage of the Internet Explorer is that CPU Use resources . But don't let one CPU If you are too busy, you can't let others CPU Idle , This strategy to ensure the workload fairness of multiple processing units is called load balancing .Node The default mechanism is to adopt the operating system preemptive policy . Generally speaking, this kind of preemption is fair to everyone , Each process can preempt according to its busy degree . But for Node for , What needs to be distinguished is that his busyness is caused by CPU、I/O It's made up of two parts , What affects preemption is CPU Busy degree of . For different businesses, there may be I/O Busy , and CPU When you're free , Thus, the load is unbalanced . So Node stay V0.11 This paper provides a new strategy to make load balancing more reasonable , This new strategy is called Round-Robin, It is also called round call scheduling . The working mode of round robin scheduling is that the main process accepts the connection , Distribute them to the work process in turn .
Cluster modular
child_process Modules tend to be lower level , later Node Provides cluster modular , To solve multi-core CPU The utilization of , At the same time, it also provides a more perfect API, To deal with the robustness of the process .
cluster.isMaster
# If the process is the main process , Then for true. This is from process.env.NODE_UNIQUE_ID Decisive . If process.env.NODE_UNIQUE_ID Undefined , be isMaster by true.
cluster.isWorker
# If the process is not the main process , Then for true( And cluster.isMaster contrary ).
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(` The main process ${process.pid} Running `);
// Derivative work process .
for (let i = 0; i < numCPUs/2; i++) {
cluster.fork();
}
cluster.on('online', ()=> {
console.log(' A child process appears ');
})
cluster.on('exit', (worker, code, signal) => {
console.log(` Working process ${worker.process.pid} Exited `);
});
} else {
// The worker process can share anything TCP Connect .
// In this case , What we share is HTTP The server .
http.createServer((req, res) => {
console.log(`${process.pid} Work `)
res.writeHead(200);
res.end(' Hello world \n');
}).listen(8888);
console.log(` Working process ${process.pid} Started `);
}Cluster How it works
in fact cluster Namely child_process and net Combined application of modules .cluster Startup time , As we said before , He will start it internally TCP The server , stay cluster.fork() Child process , Put this TCP Server side socket Send the file descriptor to the work process . If the process fork Copied out , Then there is... In its environment variable NODE_UNIQUE_ID, If there is... In the process of work listen() Listen for port calls , It will get the file descriptor , Reuse ports , Thus, multiple sub processes share ports .
边栏推荐
- SLSA: 成功SBOM的促进剂
- Website construction column setting form which website construction company is better
- Get and post are nothing more than TCP links in nature?
- 蚂蚁集团自研TEE技术通过国家级金融科技产品认证
- How to create a virtual server through a fortress machine? What are the functions of the fortress machine?
- Industry 4.0 era: the rise of low code may bring about changes in the pattern of manufacturing industry
- 0day1- (cve-2021-44228) log4j2 rce recurrence
- Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
- 专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
- How to set dynamic background for website construction what are the benefits of dynamic background
猜你喜欢

openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区

Slsa: accelerator for successful SBOM

Why is only one value displayed on your data graph?

Hackinglab penetration test question 8:key can't find it again

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse

脚本之美│VBS 入门交互实战

为什么你的数据图谱分析图上只显示一个值?

Ant group's self-developed tee technology has passed the national financial technology product certification

SAVE: 软件分析验证和测试平台
随机推荐
Virtual machine performance monitoring and fault handling commands on the console
為什麼你的數據圖譜分析圖上只顯示一個值?
In the new easygbs kernel version, the intranet mapping to the public network cannot be played. How to troubleshoot?
Save: software analysis, verification and test platform
Service API version design and Practice
[technical dry goods] the technical construction route and characteristics of zero trust in ant Office
Kubecon2021 video collection
Mysql中的触发器定义及语法介绍
Slsa: accelerator for successful SBOM
Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival
[tcapulusdb knowledge base] example of deleting data (TDR table)
Usage of cobaltstrike: Part 1 (basic usage, listener, redirector)
Integrated management and control system of 3D intelligent warehousing and logistics park
Talking about using email to attack social engineering
What is the difference between RosettaNet, EDI ANSI X12 and EDIFACT
Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse
How does H5 communicate with native apps?
What should be done when the encryptor fails to authenticate in the new version of easycvr?