If you’re looking for a job with Node.js, but don’t know where to start to measure your mastery of Node.js. In this article, we’ve listed 10 common Node.js interview questions and examined the main aspects of Node.js programming.
Before entering the main body, there are two things to declare in advance:
- These questions are only a part of node.js knowledge system, and can not fully assess the practical development ability of the interviewees.
- Real world development problems require improvisation 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 listen on port 80?
- What is an event loop?
- What tools can be used to ensure a consistent programming style?
- What’s the difference between an arithmetic error and a programmer error?
- What are the benefits of using NPM?
- What is a stub? What’s a usage scenario?
- What is a test pyramid? How do I leverage the test pyramid for the HTTP API?
- What is your favorite HTTP framework and why?
Now, let’s answer each of these questions in turn.
What is an error-first callback function?
Error-first callback functions are used to pass errors and data. The first argument should always be an error object that checks for errors in the program. The remaining parameters are used to pass data. Such as:
fs.readFile(filePath, function(err, data) {
if (err) {
//handle the error
}
// use the data object
});
Copy the code
This topic is intended to check the interviewees’ basic knowledge of Node asynchronous operations.
How to avoid callback hell
Here are a few things you can do:
- Modularity: Split the callback function into separate functions
- Use the Promises
- use
yield
To compute generators or promises
There are many different answers to this question, depending on the scenario you are using, such as ES6, ES7, or some control flow library.
How do I use Node to listen on port 80
There’s a catch! You should not attempt to listen on port 80 on unix-like systems, as this requires superuser privileges. It is therefore not recommended that your application listen directly on this port.
Currently, if you must have your application listen on port 80, you can do so by adding a layer of reverse proxy (such as Nginx) on the front of the Node application, as shown below. Otherwise, you are advised to listen on ports greater than 1024.
Review images
Direction proxy refers to a proxy server that receives connection requests on the Internet, forwards the requests to a server on the internal network, and sends the results returned by the server to the client.
For more information on reverse proxies, I suggest you read this article.
Explanation: This question is used to check if the candidate has actual experience running Node applications.
What is an event loop
Node uses a single-threaded processing mechanism (all I/O requests work in a non-blocking manner), at least from the node.js developer’s perspective. At the bottom, Node.js uses Libuv as an abstract encapsulation layer to shield the differences between different operating systems. Node can use Livuv to achieve multi-threading. The following diagram shows the relationship between Node and Libuv.
Review images
The Libuv library is responsible for executing the Node API. It assigns different tasks to different threads, forming an event loop that asynchronously returns the results of the tasks to the V8 engine. It can be represented simply by the picture below.
View pictures (pictures from the Internet)
Every I/O needs a callback function that is pushed to the event loop for execution once it is finished. If you need a more detailed explanation, check out 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 a consistent code style
You can choose from the following tools:
- JSLint
- JSHint
- ESLint
- Recommend JSCS –
In team development, these tools can be very helpful for writing code, helping team developers enforce prescribed style guidelines, and catching common errors through static analysis.
Is the interviewee experienced in large-scale project development?
The difference between arithmetic errors and programmer errors
Arithmetic errors are not bugs, they are system-specific problems, such as request timeouts or hardware failures. And programmer errors are called bugs.
This topic is not related to Node. It is used to test the basic knowledge of the interviewer.
What are the benefits of using NPM?
With NPM, you can install and manage dependencies for your project and specify the version numbers of dependencies. For Node application development, you can use package.json files to manage project information, configure scripts, and specify which versions the project depends on.
For more information about NPM, you can refer to the official documentation.
It examines the basic knowledge of using NPM commands and the practical experience of node.js development.
What is a Stub? Here’s a usage scenario
A Stub is a function or program that simulates a component or module. In simple terms, in test cases, you can use stubs to simulate a method without calling real methods, and you can return imaginary results. You can use stubs with assertions.
For example, in a file-reading scenario, when you don’t want to read a real file:
var fs = require('fs');
var readFileStub = sinon.stub(fs, 'readFile', function (path, cb) {
return cb(null, 'filecontent');
});
expect(readFileStub).to.be.called;
readFileStub.restore();
Copy the code
In unit tests: Stubs completely simulate an external dependency, while mocks are often used to determine whether a test passes or fails.
You can refer to this link for a summary of Unit tests for Node.js.
The test is used to test whether the interviewee has test experience. If the interviewee knows what a Stub is, go ahead and ask him how he did the unit test.
What is a test pyramid?
The test pyramid means that when we write test cases, there should be far more unit tests at the bottom than end-to-end tests at the top.
Review images
When we talk about the HTTP API, we may refer to:
- There are many low-level unit tests for the model
- But when you need to test how models interact, do you need to reduce integration testing
This passage mainly examines the interviewee’s experience in testing.
Your favorite HTTP framework and why
There is no single answer to this question. This topic mainly examines the intervieee’s understanding of the Node framework he uses, and whether he can give reasons for choosing this framework, advantages and disadvantages. You can refer to this website for common HTTP frameworks.
Statement
The original address: https://blog.risingstack.com/node-js-interview-questions/
References
- http://zyzhang.github.io/blog/2013/04/28/test-pyramid/
- http://www.ruanyifeng.com/blog/2014/10/event-loop.html
- http://segmentfault.com/a/1190000002921481