This time I bring you front-end interview questions about Node.js. What should you pay attention to in the front-end interview about Node.js positions? Here are the practical questions, let’s take a look.
[Related recommendations: Front-end interview questions (2020)]
If you want to find a job related to Node.js, but don’t know where to start with the evaluation Your own mastery of Node.js. This article lists 10 common Node.js interview questions for you, and examines several major aspects related to Node.js programming.
Before entering the text, two points need to be stated in advance:
These questions are only a part of the Node.js knowledge system and cannot fully examine the actual development capabilities of the interviewee.
Problems encountered in real-world development require adaptability and teamwork, so you can try pair programming.
List of Node.js interview questions
What is an error-first callback function?
How to avoid callback hell?
How to use Node to monitor port 80?
What is an event loop?
What tools can be used to ensure a consistent programming style?
What is the difference between operational errors and programmer errors?
What are the benefits of using NPM?
What is a stub? Give me a usage scenario?
What is the testing pyramid? How to leverage the testing pyramid for HTTP APIs?
What is your favorite HTTP framework and why?
Now, let’s answer these questions in turn.
What is the error-first callback function?
The error-first callback function is used to pass errors and data. The first parameter should always be an error object, used to check whether an error occurred in the program. The remaining parameters are used to pass data. For example:
fs.readFile(filePath, function(err, data) { if (err) { //handle the error } // use the data object});
Analysis: The main function of this question is to check the interviewee's mastery of some basic knowledge of asynchronous operations in Node.
How to avoid callback hell
You can have the following methods:
Modularization: Split the callback function into Standalone functions
Use Promises
Use yield
to calculate generators or Promise
Parsing: There are many answers to this question, depending on what you use Scenarios, such as ES6, ES7, or some control flow libraries.
How to use Node to monitor port 80
There is a trap in this question! On Unix-like systems you should not try to listen on port 80, as this requires superuser privileges. Therefore it is not recommended to let your application listen directly to this port.
Currently, if you must let your application listen to port 80, you can achieve this by adding another layer of reverse proxy (such as nginx) in front of the Node application, as shown in the figure below. Otherwise, it is recommended that you directly listen to a port greater than 1024.
Directional proxy refers to using a proxy server to receive connection requests on the Internet, then forwarding the requests to the server on the internal network, and sending the results returned by the server to the client.
For more information about reverse proxy, I recommend you read this article. For the practice of how to use nginx to configure the direction proxy for node, you can refer to this blog post.
Explanation: This question is used to check whether the interviewee has actual experience running Node applications.
What is the event loop
Node uses a single-threaded processing mechanism (all I/O requests use non-blocking working methods), at least from Node This is the perspective of a .js developer. At the bottom level, Node.js uses libuv as an abstract encapsulation layer to shield the differences between different operating systems. Node can use livuv to implement multi-threading. The following figure shows the relationship between Node and libuv.
Libuv library is responsible for the execution of Node API. It allocates different tasks to different threads to form an event loop, and returns the execution results of the tasks to the V8 engine in an asynchronous manner. It can be simply represented by the picture below.
Every I/O requires a callback function - once executed, it is pushed to the event loop for execution. If you need a more detailed explanation, you can refer to this video. You can also refer to this article.
Explanation: This is used to check the underlying knowledge of Node.js, such as what libuv is and what it does.
What tools can be used to ensure consistent coding style
You can choose the following tools:
JSLint
JSHint
ESLint
JSCS - Recommended
In team development, these tools are very helpful for writing code, helping team developers enforce the prescribed style guide, and also Ability to catch common errors through static analysis.
Analysis: Used to check whether the interviewee has experience in large-scale project development.
The difference between operational errors and programmer errors
运算错误并不是bug,这是和系统相关的问题,例如请求超时或者硬件故障。而程序员错误就是所谓的bug。
解析:这个题目和Node关系并不大,用于考察面试者的基础知识。
使用NPM有哪些好处?
通过NPM,你可以安装和管理项目的依赖,并且能够指明依赖项的具体版本号。 对于Node应用开发而言,你可以通过package.json文件来管理项目信息,配置脚本, 以及指明项目依赖的具体版本。
关于NPM的更多信息,你可以参考官方文档。
解析:它能考察面试者使用npm命令的基础知识和Node.js开发的实际经验。
什么是Stub?举个使用场景
Stub是用于模拟一个组件或模块的函数或程序。在测试用例中, 简单的说,你可以用Stub去模拟一个方法,从而避免调用真实的方法, 使用Stub你还可以返回虚构的结果。你可以配合断言使用Stub。
举个例子,在一个读取文件的场景中,当你不想读取一个真正的文件时:
var fs = require('fs');var readFileStub = sinon.stub(fs, 'readFile', function (path, cb) { return cb(null, 'filecontent'); }); expect(readFileStub).to.be.called; readFileStub.restore();
在单元测试中:Stub是完全模拟一个外部依赖,而Mock常用来判断测试通过还是失败。
有关Node.js的单元测试小结,你可以参考这个链接。
解析:用于测试被面试者是否有测试的经验。如果被面试者知道什么是Stub, 那么可以继续问他是如何做单元测试的。
什么是测试金字塔?
测试金字塔指的是: 当我们在编写测试用例时,底层的单元测试应该远比上层的端到端测试要多。
当我们谈到HTTP API时,我们可能会涉及到:
有很多针对模型的底层单元测试
但你需要测试模型间如何交互时,需要减少集成测试
解析:本文主要考察被面试者的在测试方面的经验。
你最喜欢的HTTP框架以及原因
这题没有唯一的答案。本题主要考察被面试者对于他所使用的Node框架的理解程度, 考察他是否能够给出选择该框架的理由,优缺点等。常用的HTTP框架你可以参考这个网站。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
一个用Vue.js 2.0+做出的石墨文档样式的富文本编辑器
The above is the detailed content of Front-end interview questions about Node.js. For more information, please follow other related articles on the PHP Chinese website!