All posts
nodejs14 min read

Node.js Developer Technical Interview (Basic)

If you have beem preparing for the technical interview for Node.js developer position or you just want to affirm your Node.js knowledge feel free to read and refresh your memory.

Introduction

Every job change requires at least some effort. Have you ever been in situation that you cannot remember some basics at the moment, but you are sure you know it? Do not worry, we all have been there. It is good practice to remind ourselves from time to time about things we know but have lost our minds by absorbing a mountain of new information. So I won't bother you anymore, lets dive into Node.js

What is Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment and library that runs on the Chrome’s V8 JavaScript engine (which is written in c++ and is highly performant ) and allows you to create and run server-side web applications outside and independent of the client’s browser. It is written in C, C++, Javascript . Node uses event-driven, non-blocking I/O model which makes it lightweight and pretty fast. It is using single threaded event loop architecture to handle many concurrent clients.

Node.js is perfect for data-intensive applications as it uses an asynchronous, event-driven model, as well as for large scale application development, single page applications,APIs, social media applications, streaming sites, etc.

What are major benefits of using Node.js

Nodejs has a handful of advantages but we will list only the most prominent ones:

  • It is really fast, being built on V8 engine makes it very fest code execution
  • It is Asynchronous, Node.js server never waits API to return response so it makes it async
  • It is highly scalable,capable to handle a huge number of concurrent connections and support both vertical and horizontal scaling
  • Huge community, great deal of modules so we don't have to invent hot water
  • There is no buffering in Node.JS application it simply output the data in chunks
  • Last but not least, it is free (Open source)

How many types of API functions are there in Node.js

There is two types of API functions:

  • Synchronous - which are blocking functions, means that functions will make system wait for a response and it will block main loop
  • Asynchronous -which are non-blocking functions, means that it will make request and continue with execution, it will not make system wait for a response

What is node REPL

The REPL Read-Eval-Print-Loop in Node.js represents interactive shell which reads code user entered, evaluate results interpreting line of code, prints results and looping until user send quit signal.

What is NPM

NPM represents two things: 1.) it is and online repository for the publishing of open-source Node.js projects 2.) it is command line utility which allows user to interact with packages and manipulate with them, which means it is used for package installation, version management, etc.

How does Node.js work

Why is Node.js single-threaded

Node.js was created explicitly as an experiment in async processing. This was to try a new theory of doing async processing on a single thread over the existing thread-based implementation of scaling via different frameworks.

Important to note, Node.js is single-threaded but under the hood it is using libuv which is responsible for spawning many threads.

Blocking vs Non-blocking I/O

As in official Node.js documentation says:

Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. This happens because the event loop is unable to continue running JavaScript while a blocking operation is occurring. In Node.js, JavaScript that exhibits poor performance due to being CPU intensive rather than waiting on a non-JavaScript operation, such as I/O, isn't typically referred to as blocking. Synchronous methods in the Node.js standard library that use libuv are the most commonly used.All of the I/O methods in the Node.js standard library provide asynchronous versions, which are non-blocking , and accept callback functions. Some methods also have blocking counterparts, which have names that end with Sync.

When comparing codes, you can see the difference,for example blocking code:

const fs = require('fs');
const data = fs.readFileSync('/test.md');
}

on the other hand below you can see non-blocking code:

const fs = require('fs');
fs.readFile('/test.md', (err, data) => {
  if (err) throw err;
});
}

What is libuv

We said earlier that Node.js is single-threaded, but how may we create multiple workers to load of thread if they are not thread ? Answer is libuv, which is special libary built (in the beginning) specially for Node. It is build in C language, uses system kernel which has multiple threads. Specifically, node do not use more than one thread, but libuv is, libuv spawns workers who are in fact threads

Concurrency and Throughput

JavaScript execution in Node.js is single threaded, so concurrency refers to the event loop's capacity to execute JavaScript callback functions after completing other work. Any code that is expected to run in a concurrent manner must allow the event loop to continue running as non-JavaScript operations, like I/O, are occurring. This is because libuv sets up a thread pool to handle such concurrency. How many threads will be there in the thread pool depends upon the number of cores but you can override this.

What is event loop

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises. The event loop allows Node.js to perform non-blocking I/O operations despite the fact that JavaScript is single-threaded. It is done by assigning operations to the operating system whenever and wherever possible.

Representation of event loop

Representation of event loop

So when an async function needs to be executed(or I/O) the main thread sends it to a different thread allowing v8 to keep executing the main code. Event loop involves different phases with specific tasks such as timers, pending callbacks, idle or prepare, poll, check, close callbacks with different FIFO queues. Also in between iterations it checks for async I/O or timers and shuts down cleanly if there aren't any.

Fork and Cluster

What is fork in node JS

Although NodeJS is a single-threaded JavaScript runtime, it can create multiple subprocesses that run separately from each other, allowing you to divide and run your application script from different NodeJS instances.

The child process will have its own memory and runtime instance, so you need to have additional resources to allocate for each child process.

The NodeJS fork() method is a built-in function of the child_process module that allows you to create a child process that’s connected to the main process currently running your code.

The fork() method accepts the following three parameters:

A module path string for a JavaScript file to execute on the child process (required) An array of string to pass as the child process arguments The options object that you want to pass to the child process

What is cluster

Node.js applications run on a single processor, which means that by default they don’t take advantage of a multiple-core system. Cluster mode is used to start up multiple node.js processes thereby having multiple instances of the event loop. When we start using cluster in a nodejs app behind the scene multiple node.js processes are created but there is also a parent process called the cluster manager which is responsible for monitoring the health of the individual instances of our application.

Worker threads vs Clusters

Cluster: Clustering is a way to load-balance incoming requests to your nodejs server over several copies of that server. There is one process on each CPU with an IPC to communicate. In case we want to have multiple servers accepting HTTP requests via a single port, clusters can be helpful. The processes are spawned in each CPU thus will have separate memory and node instance which further will lead to memory issues.

Worker threads:

Worker threads are a way for a single nodejs process to offload long-running functions to a separate thread, to avoid blocking its own main loop.

There is only one process in total with multiple threads. Each thread has one Node instance (one event loop, one JS engine) with most of the APIs accessible. Shares memory with other threads (e.g. SharedArrayBuffer) This can be used for CPU-intensive tasks like processing data or accessing the file system since NodeJS is single-threaded, synchronous tasks can be made more efficient leveraging the worker's threads.

What is Middleware

Middleware comes in between your request and business logic. It is mainly used to capture logs and enable rate limit, routing, authentication, basically whatever that is not a part of business logic. There are third-party middleware also such as body-parser and you can write your own middleware for a specific use case.

Describe the exit codes of Node.js

Exit codes give us an idea of how a process got terminated/the reason behind termination.

  • Uncaught fatal exception - (code - 1) - There has been an exception that is not handled

  • Unused - (code - 2) - This is reserved by Bash for builtin misuse

  • Fatal Error - (code - 5) - There has been an error in V8 with stderr output of the description

  • Internal JavaScript Evaluation Failure - (code - 4) - There has been an exception when the bootstrapping process failed to return function value when evaluated.

  • Internal Exception handler Run-time failure - (code - 7) - There has been an exception when bootstrapping function was called

What is an Event Emitter in Node.js

EventEmitter is a Node.js class that includes all the objects that are basically capable of emitting events. This can be done by attaching named events that are emitted by the object using an eventEmitter.on() function. Thus whenever this object throws an even the attached functions are invoked synchronously.

const EventEmitter = require("events");
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on("event", () => {
   console.log("an event occurred!");
});
myEmitter.emit("event");

What is Node.js, and what are its main features? How does Node.js handle asynchronous programming? What is the Event Loop in Node.js, and how does it work? Can you explain the difference between callback functions, promises, and async/await in Node.js? What is npm, and what is its role in Node.js development? Explain the purpose of package.json in a Node.js project. How do you handle errors in Node.js applications? What is middleware in the context of Express.js, and how is it used? Can you describe the purpose of the "require" function in Node.js? How do you manage dependencies in a Node.js project? What are streams in Node.js, and why are they useful? Explain the role of modules in Node.js, and how do you create and use them? What is the purpose of the "exports" and "module.exports" objects in Node.js modules? How can you debug a Node.js application? What is clustering in Node.js, and how can it improve application performance? How do you securely handle user authentication and authorization in a Node.js application? Can you explain the concept of RESTful APIs and how they are implemented in Node.js? What are some best practices for optimizing the performance of a Node.js application? How do you deploy a Node.js application to a production environment? Can you discuss some common security vulnerabilities in Node.js applications and how to mitigate them?

Question: What is Node.js, and what are its main features?

Answer: Node.js is a JavaScript runtime environment built on Chrome's V8 JavaScript engine. Its main features include:

Asynchronous and event-driven: Node.js uses non-blocking, event-driven architecture, making it efficient for handling I/O operations and scalable for concurrent connections. Single-threaded but highly scalable: Node.js operates on a single-threaded event loop model, allowing it to handle a large number of concurrent connections without the overhead of traditional multi-threaded approaches. JavaScript everywhere: Node.js enables developers to use JavaScript for both server-side and client-side development, providing consistency and code reuse across the stack. NPM ecosystem: Node.js comes with npm (Node Package Manager), the largest ecosystem of open-source libraries and tools, facilitating rapid development and sharing of reusable code. Lightweight and fast: Node.js has a small footprint and low overhead, making it fast to install, deploy, and execute, ideal for building high-performance web applications.

  • What is Node.js, and how does it differ from other server-side technologies? Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, allowing developers to run JavaScript on the server-side. Unlike traditional server-side technologies like PHP or Java, Node.js is event-driven and non-blocking, making it highly scalable and efficient for handling I/O-heavy applications.

  • Explain the concept of event-driven programming in Node.js. Event-driven programming in Node.js means that the server responds to events such as HTTP requests, file system operations, or database queries asynchronously. Instead of blocking and waiting for each operation to complete, Node.js registers callback functions to be executed when the operation completes, allowing the server to handle multiple requests concurrently.

  • What is the Event Loop in Node.js, and how does it work? The Event Loop is the core mechanism in Node.js that enables asynchronous I/O operations. It continuously checks the event queue for pending events and executes the associated callback functions in a non-blocking manner. This allows Node.js to handle multiple concurrent operations efficiently without getting blocked.

  • What are the benefits of using npm in Node.js development? npm (Node Package Manager) is the largest ecosystem of open-source libraries and tools for Node.js. It simplifies dependency management, allows easy installation and updating of packages, facilitates code reuse, and provides access to a vast collection of modules for various functionalities, speeding up development and reducing project complexity.

-Explain the difference between callback functions, promises, and async/await in Node.js. Callback functions are functions passed as arguments to other functions and executed later, commonly used for handling asynchronous operations in Node.js. Promises provide a cleaner and more readable way to work with asynchronous code by representing a future value or error, allowing chaining of operations and better error handling. async/await is a syntactic sugar built on top of promises, making asynchronous code look and behave more like synchronous code, enhancing readability and maintainability.

  • How does error handling work in Node.js applications? Error handling in Node.js involves using try/catch blocks for synchronous code and handling errors using callback functions or promise rejections for asynchronous code. Additionally, Node.js provides the 'error' event for handling uncaught exceptions at the process level.

  • What are streams in Node.js, and why are they useful? Streams are objects used to read or write data continuously in Node.js, enabling efficient processing of large amounts of data without loading it entirely into memory. They are useful for tasks like file I/O, network communication, and data transformation, improving performance and reducing memory usage.

  • Explain the role of middleware in Express.js. Middleware functions in Express.js are functions that have access to the request and response objects and can modify them or terminate the request-response cycle. They are used to perform tasks such as logging, authentication, data parsing, and error handling, allowing for modular and reusable code in Express.js applications.

  • How do you securely handle user authentication and authorization in Node.js applications? Secure user authentication and authorization in Node.js applications involve using best practices such as password hashing, session management, JSON Web Tokens (JWT) for authentication, role-based access control (RBAC), and HTTPS for secure communication, to protect against common security threats like SQL injection, cross-site scripting (XSS), and cross-site request forgery (CSRF).

  • What are some common performance optimization techniques for Node.js applications? Performance optimization in Node.js applications can be achieved through techniques such as caching, code profiling and optimization, using efficient algorithms and data structures, minimizing blocking operations, leveraging clustering for multi-core systems, employing load balancing, and using CDN (Content Delivery Network) for static assets, to enhance throughput, response times, and scalability.