Node. JS interview questions and Answers (version 2017)

Source: Internet
Author: User

Read the original

Two years ago, we released our first article on Node. JS interview FAQ and answer, the JavaScript and Node. JS Ecosystem has had a lot of updates and developments in the last two years, so it's time to update these interview questions and Answers.

Disclaimer (important)

It's never a good choice to evaluate an interviewer just by interviewing Questions. But these questions can give you a general idea of the Interviewer's experience with Node. JS Development .

Of course, These problems do not fully reflect a person's way of thinking and development habits.

I believe that the problems encountered in development are more reflective of the interviewer's level of knowledge- so we encourage both the interviewer and the candidate to do pairing programming (pair programming refers to two programmers sitting at the same workbench developing Software).

and last but not least: we are all people, and we want to make the best possible for the interviewer to feel respected and Welcomed. These questions do not mean the problem and the correct answer, just let the interview have something to talk About.

Node. JS interview questions
    • What is the Error-first callback mode?

    • How to avoid "callback hell"?

    • What is Promises?

    • What tools unify the code style of the team? Why is a unified code style important?

    • When should I use npm? When should I use yarn?

    • What is the pile code (stub)? Please describe an application scenario!

    • What is a test pyramid? Please illustrate!

    • What is the HTTP framework you admire most? Why?

    • What if you keep your cookie safe? How do I block XSS attacks?

    • How do I confirm the dependency security of the project?

Questions and Answers What is the Error-first callback mode?

The Error-first callback pattern is applied for better error and data delivery. The first parameter is reserved to an error object, and in the event of an error, the error is returned by the first parameter Error. The rest of the parameters are used as data delivery.


How to avoid "callback hell"?

There are many ways to solve this problem:

    • Modular design: call callbacks split into separate functions

    • Use Process Control libraries , such as async

    • Combined use of generators and Promises

    • Use the async/await function (can only be run in node V7 release version, node LTS version is not yet supported-want to know more, can refer to our blog async/await practice)

What is Promises?

The concept of promise, which was introduced in the 80 's, is now a feature supported in most modern programming languages, making it easier to implement asynchronous models.

As a simple example, normally the output will be after 100ms result . Once failed, an catch exception can be thrown. Promises allows chain Operation.


What tools unify the code style of your team? Why is unified code style important?

Consistent code style is important when teamwork, and code style is consistent, and team members can build projects more easily without worrying about using new code styles for every build Project.

At the same time, a consistent code style can be used to troubleshoot code problems through static Analysis.

Tools to help standardize your code:

    • ESLint

    • Standard

If you want to go further, I recommend that you immediately learn the JavaScript clean Coding principle.

What is the pile code (stub)? Please describe an application scenario!

The stub is the code that simulates some function in some component or module. The function of the stub is to occupy the position and let the code run smoothly during the Test.

An example of the actual function is to write a file, but this code does not really do this thing.


What is a test pyramid? Please illustrate!

The test pyramid describes the unit test, the integration test (integration tests), and the percentage of end-to-end testing (end-to-end Test) in the Test.

As an example, testing an HTTP API requires:

    • A lot of unit tests on models (using pile code Processing),

    • Some integration tests on how models interacts with other models (without the use of pile code Processing),

    • A small amount of end-to-end testing, that is, Real-world calls (not using pile code Processing).

what is the HTTP framework you admire most? Why?

There is no answer to this Question. The goal of this problem is to see how much the interviewer understands the frame he is using. As long as the interviewer answers the pros and cons of the framework, Why use this framework is Possible.

When should I use the messaging service in a background process? How do I handle the task of the worker thread/how to schedule a task for the worker?

Message Queuing is used for background data transfer services, such as sending mail and data image processing.

Message Queuing has a number of solutions, such as RabbitMQ and Kafka.

How to keep your cookie safe? How do I block XSS attacks?

An XSS attack is an attacker who inserts malicious JAVASCRIPT code into an HTML page.

To prevent attacks, you need to handle the HTTP header set-cookie :

    • HttpOnly -this Property helps prevent cross-site scripting attacks, which prohibit access to cookies through JavaScript.

    • Secure -this property tells the browser that only cookies are allowed to be accessed over the HTTPS Protocol.

So what you need to do is write in the request Set-Cookie: sid=; HttpOnly . If you are using the Express framework, you can use the Express-cookie session, which will default to the settings above to prevent XSS attacks.

How do I confirm the dependency security of the project?

With Node. JS development, There are hundreds of dependencies that can be easily seen.

For example, If you rely on Express, to say exactly what it means to also rely on 27 other modules, manually detecting updates to these modules is not a choice!

The only option is to automatically secure updates to your dependencies and you have the following free or paid options:

    • npm outdated

    • Trace by Risingstack

    • Nsp

    • Greenkeeper

    • Snyk

Node. JS Answers

The following section focuses on solving some confusing and tricky issues for the candidate who is preparing for Node. Js.

what's wrong with the following code?

Answer

then after no catch , no catch Exception. Doing so causes a failure to silence and does not throw an Exception.

To refine this code, we can do the following:


You can use this tool if you are debugging a huge codebase and have a potential problem with a Promise function than you do not know unhandledRejection . It will print out the Promise of all the unhandled reject States.


what's wrong with the following code?

Answer

In the case of data validation (security credentials), It is most important to avoid any information disclosure. therefore, we want to control the execution time of the data check. We want to make sure that regardless of the data passed, we validate that the data is consumed in the same time. If you can't do this, your program's resistance to timing attacks is LOW.

Why is there such a phenomenon?

Node. JS uses the JavaScript V8 engine, developed for high-speed web pages, and has excellent performance. The V8 engine compares data in a byte comparison, and once a byte is found to be inconsistent, the comparison operation Stops. as a result, the longer the attacker passes in the password checksum, the more the correct portion of the password is Indicated.

To fix this problem, you can use the NPM module Cryptiles.


what is the output of the following code?

Answer

The answer is 2 -like this , I recommend that the interviewer explain what the results of each line of code are, so that the examiner can tell how the interviewer is thinking , such as:

    1. Creates a Promise object that outputs1

    2. Result +1, now the output is2

    3. The output is discarded and an error is thrown

    4. Error is discarded, returning a new value ( 1 )

    5. Catch performs exception handling, but the program does not stop after a catch, returning a message after +1 of value ( 2 )

    6. Value Normal output

    7. Value is output normally, This line does not execute

spending time with one day is more effective than technical questioning

It is more valuable to spend at least half a day with the interviewer than to test 1000

If you do this, you can learn more about the Interviewer's ability to adapt to the culture of the company and whether it has the skills to fit the Position.

Node. JS interview questions and Answers (version 2017)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.