- Before you bury yourself in packages, learn the Node.js runtime itself
- This article has been authorized by author Samer Buna
- The Nuggets translation Project
- Translator: FGHPDF
- Proofreader: RCcoder, REid3290
Before you indulge in a sea of packages, take a look at runtime Node.js itself
This article will push the limits of your Node.js knowledge.
I started learning Node.js shortly after Ryan Dahl first introduced it, and even a year ago I couldn’t answer many of the questions I posed in this article. If you can truly answer all the questions, your Knowledge base on Node.js is radically different. We should be friends.
The reason FOR this challenge may surprise you, but many of us have been learning Node the wrong way. Most tutorials, books, and courses on Node focus on the Node ecosystem, not Node itself. They focus on teaching you all the Node packages to use, such as Express and socket. IO, rather than teaching you how to use Node itself.
And there are good reasons to do so. Node is native and flexible. It does not provide a complete solution, but a rich solution that you can implement yourself. Libraries like Express.js and socket.io are more complete solutions, so it makes more sense to teach these libraries so that learners can use these complete solutions.
The conventional wisdom seems to be that only those who write class libraries such as Express.js and socket.io need to know everything about the Node.js runtime. But I think that’s wrong. A deep understanding of Node.js itself is the best thing to do before using these complete solutions. You should at least have enough knowledge and confidence to judge from a package’s code whether you should learn to use it or not.
That’s why I decided to create a Pluralsight course that focuses entirely on Node itself. As I prepare for the lesson, I’ll list some specific questions to determine if your knowledge of Node itself is deep enough, or if you need to improve.
If you can answer these questions and are looking for a job, please contact me! On the other hand, if most of these questions confuse you, you’ll need to learn Node itself first. What you learn will make you a better developer.
Node.js Knowledge Challenges:
Some of these questions are short and easy, while others require longer answers and deeper knowledge. They are in no particular order.
I know you’ll want answers after reading this list. There are some answers in the suggestions section below, but I will also answer all these questions after this freeCodeCamp article. But let me test you out!
- What is the relationship between Node.js and V8? Can YOU run Node without V8?
- When you declare a global variable in any node.js file, is it truly global for all modules?
- When exposing the API of a Node module, why do we sometimes use it
exports
sometimesmodule.exports
? - Can we rely on local files that do not use relative paths?
- Can you use different versions of the same package in the same application?
- What is an event loop? Is it part of V8?
- What is the call stack? Is it part of V8?
setImmediate
和process.nextTick
What is the difference between?- How to make an asynchronous function return a value?
- Can callbacks be used with promises? Are they the same way or are they two different ways?
- What Node module is implemented by many other Node modules?
spawn
,exec
和fork
What are the main differences between?- How does the cluster module work? How is it different from using load balancing?
--harmony-*
What is the sign?- How to read and check the memory usage of Node.js processes?
- What does Node do when both the call stack and the event loop queue are empty?
- What are V8 objects and function templates?
- What is Libuv and how does Node.js use it?
- How to make Node’s REPL always use JavaScript strict mode?
- What is the
process.argv
? What kind of data does it have? - How do we do one last operation before the Node process terminates? Can this operation be done asynchronously?
- What built-in commands can you use in Node REPL?
- Besides V8 and Libuv, what other external dependencies do Node have?
- process
uncaughtException
What is the problem with the event? It andexit
What is the difference between events? - In the Node ‘s REPL
_
What does it mean? - Does Node Buffer use V8 memory? Can I resize them?
Buffer.alloc
和Buffer.allocUnsafe
What is the difference between?slice
What’s the difference between buffer and array?string_decoder
What is the use of modules? How is it different from buffer to string?- What are the five main steps the require function needs to perform?
- How do I check whether a local module exists?
package.json
的main
What’s the use of attributes?- What are module cycle dependencies in Node and how can they be avoided?
- What are the three file extensions that the require function automatically tries?
- When you create an HTTP service and respond to a request, why
end()
The function is required, right? - When is a file system appropriate
*Sync
Methods? - How do I print only one level of deeply nested objects?
node-gyp
What’s the use of a bag?- object
exports
,require
和module
They are global in all modules but they are different in every module. How is this done? - If you implement a only
console.log(arguments);
What does the actual Node output look like? - How can a module be used by other modules at the same time and pass
node
Command execution? - Take an example of a built-in stream that can be read and written.
- What happens when cluster.fork() is executed in the Node script?
- What is the difference between using event emitters and using simple callback functions to allow asynchronous processing of code?
console.time
What’s the function for?- What is the difference between “paused” and “flowing” modes for readable streams?
--inspect
What are the parameters for the node command?- How do I read data from a connected socket?
require
A function always caches modules on which it depends. What can you do if you need to execute code in a required module multiple times?- When using streams, when do you use pipes and when do you use events? Can these two methods be combined?
I took the best approach to learning Node.js
Learning Node.js can be challenging. Here are some guidelines to help you on this journey:
Learn the good parts of JavaScript and learn its modern syntax (ES2015 and later)
Node is a library for compiling JavaScript based on the VM engine, so it goes without saying that the important features of JavaScript themselves are a subset of the important features of Node. So you should start your learning journey with JavaScript itself.
Do you understand functions, scope, the bind keyword and the new keywords, closures, classes, module patterns, stereotypes, callbacks, and promises? Do you know the various methods you can use on Number, String, Array, Set, Object, and Map? Adapting to the items on this list will make learning the Node API much easier. For example, trying to learn the ‘fs’ module method before you have a good understanding of callbacks can lead to unnecessary confusion.
Understand the non-blocking nature of Node
Callbacks and promises (as well as generators/async modes) are particularly important for Nodes. Asynchronous manipulation is your first lesson in Node.
You can compare the non-blocking nature of a few lines of code in a Node program to the way you order Starbucks coffee (in the store, not the drive-thru) :
- Order | give some Node executes instructions (a function)
- Customize your order, such as no whipped cream | some parameters to the function:
({whippedCream: false})
- Starbucks employees tell you on your order command | told the Node execution by the callback function:
({whippedCream: false}, callback)
- Then stand aside, starbucks employees will be received from the people behind you in the queue order | Node to the back of the code receiving instructions from you.
- When do you want coffee ready, starbucks staff will call your name, and to give coffee | end when your function to calculate the Node. Js will perform a callback: according to the results of the calculation
callback(result)
I wrote a blog post describing this process: Learning asynchronous programming at Starbucks
Understand the JavaScript concurrency model and how it works based on event loops
Stack, heap, and queue. If you’ve read a book on the subject and still don’t fully understand it, check out this guy, I guarantee you’ll get it.
Philip explained the event loop in the browser, but in Node.js it’s almost exactly the same thing (although there are some differences).
See how a Node process does not go into sleep and terminates when there is nothing left to do
The Node process can be idle, but it never enters the sleep state. It keeps track of all the callbacks that are waiting to be executed, and if there are no callbacks available, it simply ends. To keep The Node process running, you can use a setInterval function, as this will create a permanently suspended callback in the event loop.
Learn the global variables you can use, such as Process, Module, and Buffer
They are defined in a global variable (usually compared to the window variable in the browser). In the Node’s REPL, type global. And click the TAB to see all the available items (or simply double click the TAB on the blank line). Some of these items are JavaScript constructs (such as Array and Object). Some of these are Node library functions (such as setTimeout or console output to stdout/stderr), and some of these are Node global objects that you can use to handle certain tasks (for example, process.env can be used to read the host’s environment variables).
You should understand most of what you see in the table.
Learn what you can do with the built-in libraries that come with Node and how they focus on “networking.”
Some of them will feel familiar, such as Timers, because they also exist in the environment that browsers and Node emulate. But there is more to learn, fs, PATH, readline, HTTP, NET, Stream, Cluster… (The list above already includes them).
For example, you can use FS to read and write files, you can run streaming Web servers using “HTTP”, and you can run TCP servers and program sockets using “NET”. Node is much more powerful today than it was a year ago, and it’s getting better and better through community code submissions. Before looking for a package to use for your task, make sure you can’t use Node’s built-in package to complete the task first.
The Event library is particularly important because most Node architectures are event-driven.
You can always learn more about the Node API here, so keep expanding your horizons.
Understand why Node is called Node
You build simple single-process building blocks (nodes) that can be organized using good network protocols to allow them to communicate with each other and scale to build large distributed programs. The simplified Node application didn’t come after that — that’s where the name came from.
Read and try to understand some of the code written for Node
Pick a framework, such as Express, and try to understand some of its code. Tell me what you don’t understand. I try to answer questions on Slack channels when I can.
Finally, write a Web application in Node without using any framework. Try to handle as many situations as possible, using HTML files, parsing query strings, accepting form input, and creating a terminal that responds with JSON.
Try writing chat servers, publishing NPM packages, and contributing to open source Node-based projects.
Wish you code yun Changlong!
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. Android, iOS, React, front end, back end, product, design, etc. Keep an eye on the Nuggets Translation project for more quality translations.