link1370 link1371 link1372 link1373 link1374 link1375 link1376 link1377 link1378 link1379 link1380 link1381 link1382 link1383 link1384 link1385 link1386 link1387 link1388 link1389 link1390 link1391 link1392 link1393 link1394 link1395 link1396 link1397 link1398 link1399 link1400 link1401 link1402 link1403 link1404 link1405 link1406 link1407 link1408 link1409 link1410 link1411 link1412 link1413 link1414 link1415 link1416 link1417 link1418 link1419 link1420 link1421 link1422 link1423 link1424 link1425 link1426 link1427 link1428 link1429 link1430 link1431 link1432 link1433 link1434 link1435 link1436 link1437 link1438 link1439 link1440 link1441 link1442 link1443 link1444 link1445 link1446 link1447 link1448 link1449 link1450 link1451 link1452 link1453 link1454 link1455 link1456 link1457 link1458 link1459 link1460 link1461 link1462 link1463 link1464 link1465 link1466 link1467 link1468 link1469 link1470 link1471 link1472 link1473 link1474 link1475 link1476 link1477 link1478 link1479 link1480 link1481 link1482 link1483 link1484 link1485 link1486 link1487 link1488 link1489 link1490 link1491 link1492 link1493 link1494 link1495 link1496 link1497 link1498 link1499 link1500 link1501 link1502 link1503 link1504 link1505 link1506

Node.js Interview Questions and Answers


More Readings

Basic Questions

In JavaScript, which types are pass by reference? And which types are pass by value? How to pass a variable by reference?

Simply speaking, objects are pass by reference. Basic types are pass by value. We can pass basic types by reference using boxing technique.

Pass by reference and pass by value is a basic question. It is fundamental part to understand how does JavaScript’s memory work. It is hardly to have further discussion without understanding reference.

In coding session, we use questions like how to write a json object copy function to assess candidate.

Sometimes, we ask about the difference between == and ===. And then, true or false of [1] == [1]. Without a good foundation, candidate may make a wrong conclusion because of the wrong understanding of == and ===.

More on pure javascript: Javascript Interview Questions and Answers

How to judge whether a interface is asynchronous? Is it asynchronous while a callback provided?

This is a open question, you can have your own way to judge.

Simply use the callback function is not asynchronous, IO operation may be asynchronous, in addition to the use of setTimeout and other ways are asynchronous.

How to implement a Sleep function?

1
2
3
4
5
function sleep(sleepMs) {
var start = Date.now(), expire = start + sleepMs;
while (Date.now() < expire) ;
return;
}

Node.js Modules

What is V8 module

We are not talking about V8, but V8 module in Node.js. It’s used for opening built-in events and interfaces of V8 engine in Node.js. Because these interfaces are defined by underlying part of V8, so we can’t say it’s absolutely stable.

Is the Eventemitter.emit synchronous or asynchronous?

Synchronous.

Events module is a very important core module in Node.js. There are many important core APIs in the node that depend on Events , for example, Stream is implemented based on Events, and fs, net, ‘http’ are implemented based on ‘Stream’, ‘Events’ is so important to Node.js.

The EventListener calls all listeners synchronously in the order in which they were registered. This is important to ensure the proper sequencing of events and to avoid race conditions or logic errors.
And when you destroy this class, don’t forget to destroy these emitters too , because inside the class, some listener may cause memory leak.

if a website has a interface A, and in some cases, interface A can be the endless loop, unfortunately, if you triggered this endless loop, what will be the impact on your website?

If endless loop logic trigger in your website, the whole process will be blocked, and all request will timeout, asynchronous code will never be executed, and your website will be crashed.

In Node.js environment javascript code has only one single thread. Only the current code has been executed, the process will cut into the event loop, and then pop out the next callback function from the event queue to start the implementation of the code, as long as an infinite loop can block the execution of the entire js process

How to implement an async.reduce

You need to know that reduce is analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value.

What’re the difference between child_process.fork and fork in POSIX?

In Node.js, child_process.fork() calls POSIX fork(2). You need manually manage the release of resources in the child process for fork POSIX. You don’t need to care about this problem when using child_process.fork, beacuse Node.js will automatic release, and provide options whether the child process can survive after the parent process is destroyed.

Does the death of parent process or child process affect each other? What is an orphan process?

The death of a child process will not affect the parent process. When the child process dies (the last thread of the thread group, usually when the “lead” thread dies), it will send a death signal to its parent process. On the other hand, when the parent process dies, by default, the child process will follow the death. But at this time, if the child process is in the operational state, dead state, etc., it will be adopted by process identifier 1(the init system process) and become an orphaned process. In addition, when the child process dies(“terminated” state), the parent process does not call wait() or waitpid() to return the child’s information in time, there is a PCB remaining in the process table. The child process is called a zombie process.

IPC(Inter-process communication), Before IPC channel was set up, how the parent process and the child process communicate between each other? If there is no communication, how is IPC set up?

When you create a child process via child_process, you can specify the env (environment variable) of the child process. When starting the child process in Node.js, the main process sets up the IPC channel first, then pass the fd(file descriptor) of the IPC channel to the child process via environment variable (NODE_CHANNEL_FD). Then the child process connects to the parent process via fd.

Finally, for the issue of inter-process communication (IPC), we generally do not directly ask the IPC implementation, but will ask under what conditions you need IPC, and the use of IPC to deal with any business scene.

What is Daemon Process

Daemon Process is a very basic concept of the server side. Many people may only know that we can start a process as a daemon by using tools such as pm2, but not what is a process and why using it. For excellent guys, daemon process implement should be known.

The normal process will be directly shut down after the user exits the terminal. The Process starting with & and running in the background will be shut down when the session (session group) is released. The daemon process is not dependent on the terminal(tty) process and will not be shut down because of the user exiting the terminal.

What is Buffer

Buffer is the class to handle binary data in Node.js, IO-related operations (network / file, etc.) are all based on Buffer. An instance of the Buffer class is very similar to an array of integers, but its size is fixed, And its original memory space is allocated outside the V8 stack. After the instance of the Buffer class is created, the memory size occupied by it can no longer be adjusted.

New Buffer () interface was deprecated from Node.js v6.x, The reason is that different types of parameters will return different types of Buffer objects, So when the developer does not correctly verify the parameters or does not correctly initialize the contents of the Buffer object, it will inadvertently introduce security and reliability problems to the code.

What is String Decoder

String Decoder is a module to decode buffers to strings, as a supplement to Buffer.toString, it supports multi-byte UTF-8 and UTF-16 characters. Such as:

1
2
3
4
5
6
7
8
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');

const cent = Buffer.from([0xC2, 0xA2]);
console.log(decoder.write(cent)); // ¢

const euro = Buffer.from([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro)); // €

What’s the current working directory of the process? What’s it for?

You can obtain the current working directory by using process.cwd(). It usually is the directory when the command line starts. It can also be specified at startup. File operations, etc. obtain the file by using the relative path which is relative to the current working directory.

Some of the third-party modules that can obtain the configuration look for the configuration file through your current directory. So if running the start script in the wrong directory, you will get wrong rusults. You can change working directory by using process.chdir() in your code.

How should I handle unexpected errors? Should I use try/catch, domains, or something else?

Here are the error handling methods in Node.js:

  • callback(err, data) Callback agreement
  • throw / try / catch
  • Error event of EventEmitter

What is uncaughtException

he uncaughtException event of process object will be triggered when the exception is not caught and bubbling to the Event Loop. By default, Node.js will ouput the stack trace information to the stderr and end process for such exceptions, And adding listener to uncaughtException event can override the default behavior, thus not end the process directly.

1
2
3
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});

Should we use Domain for error handling?

No.
In the early Node.js, try/catch is unable to capture asynchronous errors, And the error first callback is just an agreement, without mandatory and very cumbersome to write. So in order to catch the exception very well, Node.js introduces domain module in v0.8.

But domain also brought more new problems. Such as dependent modules can not inherit the domain you defined, Causing it can’t cover errors in dependent modules. Furthermore, Many people (especially new bie) didn’t understand memory / asynchronous processes and other issues in Node.js, they didn’t do well when using domain to process errors and let the code continue, this is likely to cause the project to be completely unserviceable (Any problems are possible, And all kinds of shit…)

And now, it is ‘deprecate’.