What is Node.js?
Node.js is a runtime environment for executing JavaScript outside the browser, particularly on web servers. It is built on Chrome’s V8 engine and is used to build scalable, event-driven server applications.
What is a runtime environment?
A runtime environment is the platform or infrastructure in which a program or script is executed. In the context of Node.js, it refers to the V8 engine, which allows you to run JavaScript outside of the browser
What is the difference between req.params and req.query?
req.params retrieves parameters embedded within the route’s path. req.query retrieves query string values appended to the URL. req.params is used for route matching, while req.query is used for filtering, pagination, and dynamic queries.
What is body-parser
body-parser is a middleware for Express.js that parses incoming request bodies before handlers. It converts raw incoming payloads, such as strings, into JSON accessible on req.body.
What security mechanisms are available in Node.js?
Node.js security mechanisms include using helmet middleware to set HTTP headers for protection against common vulnerabilities, input validation and sanitization, enabling HTTPS, handling authentication and authorization, using environment variables for secrets, and keeping dependencies updated.
What is REST?
Representational State Transfer (REST) is an architectural style for designing server interfaces. It organizes data and operations as resources accessible via stateless protocols such as HTTP.
What are the characteristics of REST?
statelessness (server does not retain client state between requests), , resource-based architecture (each piece of data is a resource with a URI), client-server architecture (clients and servers are separate), and a uniform interface (standard methods and representations for interacting with resources).
What does statelessness mean in REST APIs?
Statelessness means the server handles each request independently of other requests, and does not remember the client’s data between requests.
What is JWT and how does it enable stateless authorization?
JWT (JSON Web Token) allows for stateless authorization, meaning tokens contain all the info needed for authentication and are verified by the server’s secret signature. The server does not need to store session state, only verify the token.
How is JWT different from traditional stateful sessions?
In stateful sessions, the server keeps track of client data, maintaining session state across requests. With JWT, every request is authenticated independently using the token, allowing the server to be stateless and scalable.
What is a process in computer science?
A process is a running program with its own memory space and resources. In analogy, it’s like a restaurant where tasks are handled independently.
What is a thread?
A thread is a unit of execution within a process. A process can have multiple threads, analogous to waiters serving different tables within a restaurant.
How many threads does JavaScript use in Node.js?
JavaScript itself uses a single thread for executing code in Node.js, which means only one operation executes at a time on the main thread.
What is the call stack in JavaScript?
The call stack is a data structure that keeps track of function invocations. When a function is called, it is pushed onto the stack, and when it completes, it is popped off.
What happens if a task takes too long to complete in single-threaded JavaScript?
If a task takes too long on the single JavaScript thread, it blocks further execution—no other code can run until the blocking task finishes.
How does Node.js avoid blocking operations?
Node.js uses an event-driven, non-blocking I/O model. When an operation requires I/O (like database reads), it delegates the task to background worker threads using libuv and the event loop, allowing the main thread to remain responsive.
What does the event loop do in Node.js?
The event loop continuously checks the call stack and message queue, executing callbacks when tasks complete, enabling asynchronous operations without blocking the single thread.
How does Node.js handle heavy I/O tasks?
Heavy I/O tasks are offloaded to background worker threads managed by libuv. Upon completion, results are returned via the event demultiplexer, and the event loop dispatches the callback for further processing.
Give an analogy for Node.js’s asynchronous task handling.
Node.js is like a restaurant manager (event demultiplexer) who receives multiple orders at once and notifies waiters (callbacks) when specific food (data) is ready, so no customer (task) is left waiting for too long.
What is synchronous programming?
Synchronous programming is thread-blocking; each operation waits for the previous one to finish before starting.
What is asynchronous programming?
Asynchronous programming allows certain operations to run in the background, enabling other code to execute without waiting for them to finish.
What is a Promise in JavaScript?
A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
What is the benefit of asynchronous programming in JavaScript?
It enables non-blocking operations, allowing applications to be responsive and handle multiple tasks efficiently, especially for I/O or network-bound tasks.
How do callbacks relate to asynchronous programming?
Callbacks are functions passed as arguments to other functions, executed when an asynchronous operation completes, enabling further logic without blocking the main thread.