In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the reasons for reading the Nodejs source code". In the daily operation, I believe that many people have doubts about the reasons for reading the Nodejs source code. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the reasons for reading the Nodejs source code?" Next, please follow the editor to study!
First of all, the purpose of reading the source code is nothing more than two
1 in-depth understanding and understanding of his or some underlying principles
2 learn some excellent design ideas and practice from it.
If you don't have two goals, there's no need to look at the source code. Then let's talk about what you can get from reading the nodejs source code.
First of all, we need to have a deep understanding of what nodejs is. Everyone knows that nodejs is a js runtime. So what exactly is in nodejs? What is the meaning of each part? First of all, nodejs is composed of libuv, v8, and third-party libraries. The location of nodejs is first of all a server, so it is fine to have libuv. So what's the use of the rest of the components? The role of the third-party library is obvious, which is to reuse the existing solutions in the industry to expand some functions of nodejs, which is not the core of nodejs. The meaning of v8 is that because nodejs chooses the language js, it needs an engine for js. Otherwise, v8 is not needed, just compile libuv and third-party libraries into binary, just like nginx,redis. This is a high-level view. At the bottom, libuv is an encapsulation of operating system functions, and v8 is a js interpreter. Then the significance of looking at the nodejs source code is obvious. The source code of nodejs is vertically divided into the following three parts
1 js layer
2 C++ layer (using v8 to bridge js and libuv and some custom C++ logic)
3c layer
Read the js layer, you can understand some of the upper logic of the nodejs implementation, although the js layer finally depends on the bottom, but the js layer also has a lot of logic, understand the js layer, in the use of nodejs, you can also have a deeper understanding of what you are doing, what nodejs is doing. But the js layer is far from enough. Because he's just a shell.
So next, you have to read the C++ layer, read the C++ layer code, and learn more about how to use v8. Suppose you want to use v8 alone in your other projects in the future, then the nodejs code is a reference. So what's the point of understanding the use of v8? This problem is equivalent to using a language and then understanding the meaning of its compiler / interpreter. Most of the time, we don't need to do this, it all depends on your interest. If you want to use V8 later, or you want to know the underlying principles of the js implementation, or you want to know how a compiler / interpreter is implemented. Then you can learn v8. V8 starts as a js engine with a general logic of compiler / interpreter, lexical parsing, syntax parsing, code generation, code optimization, and so on. Then it also contains the implementation details of the js language itself, such as an js array, a js object, how it is implemented in V8. Finally, learn what interfaces v8 exposes for us and what we can do with v8.
Finally, the focus of reading libuv,nodejs is as a server, so relatively speaking, reading libuv is the key point, we all know that V8 is just a js engine, it does not have network, dns, file and other capabilities. At the front end, js's file, network and other capabilities come from the host browser. At nodejs, these capabilities come from libuv. This is why nodejs is called nodejs, not v8. Because he's not just v8. He also realized some of his own functions. So you can also implement your own functions, plus the ability of V8, to create a new server. Back to the point, what can you learn from reading libuv? libuv is the core of implementing a server. So we can learn the technology used to implement a server from libuv. We can also know from the libuv official website that libuv includes processes, threads, timers, files, tcp, udp, Unix domain, thread pool, dns, etc., while operating system capabilities include inter-process communication (pipeline, Unix domain, eventfd), thread pool, event-driven (epoll, select, poll, kqueue, etc.), inotify mechanism, file operation, etc., and use data structures and algorithms such as binary heap, red-black tree, queue, etc.
Generally speaking, the most direct way to read the nodejs source code is to understand how nodejs works and the nature of nodejs. If you are a nodejs developer, this is undoubtedly a good harvest. Secondly, if you are interested, you can also learn about compilers and operating systems. You can also learn some design ideas in nodejs, such as timer design, from early versions to later refactoring backgrounds. Or if you design a server, how do you design it.
In theory, I talked about some views on reading the nodejs source code, and by the way, I will talk about my own views and experience. The reason I read the nodejs source code is very straightforward, because I want to be a good nodejs engineer. I'm not very interested in V8 and libuv itself. Although I have always been a front-end engineer, I will not read the source code of the js engine. As for libuv, there are a lot of related asynchronous io libraries, and when it comes to server design, nginx and redis are undoubtedly more worth reading. But to be a good nodejs engineer, it is necessary to have a deep understanding of nodejs itself. Relatively speaking, nodejs is very native. Many times we think that reading the nodejs source code is meaningless because there is no depth or breadth to use nodejs, may just stay at the framework level, copy business best practices, and successfully avoid some pitfalls. When you encounter some problems, but it is difficult to solve, and even the industry does not have a solution, you will deeply understand the significance of reading the source code. Of course, this kind of time may not be very (such as https://cnodejs.org/topic/600b9de15d04ac76cf2181a7 and https://cnodejs.org/topic/6018f1b103d797fb8e66e71c#6019f31903d7976d1066e9d2, etc.).
From the beginning to read the nodejs source code to now, I think this is a very difficult, happy, profound experience. He not only made me understand and understand nodejs better, but also improved me at a higher level. Although I have been promoting reading the nodejs source code, writing a lot of articles, and communicating with other students. But it doesn't mean you have to read the source code. Personally, reading the source code is a good habit and a way to make you good, but because which source code you read, it all depends on your personal interests and choices. Don't read it blindly, be purposeful. Personally, I am very grateful to nodejs, and admire nodejs authors and contributors very much, but I think I know more about the principles of nodejs in reading the nodejs source code, and do not learn much what I want to learn. V8 is on the one hand, and libuv content, I think looking at nginx and operating system kernel may be a better choice, but libuv may be lighter and faster to understand the design of an asynchronous framework.
At this point, the study of "what are the reasons for reading the Nodejs source code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.