Node JS Question Bank Answers
Node JS Question Bank Answers
2 Marks Questions
1. What is Node.js?
Node.js is an open-source, cross-platform runtime environment that allows you to run JavaScript
code outside the browser. It uses the V8 engine and is primarily used for building fast, scalable
server-side and networking applications.
2. What is NPM?
NPM (Node Package Manager) is the default package manager for Node.js. It allows developers to
install, share, and manage dependencies (libraries, tools) needed for their Node.js projects.
The Event Loop in Node.js handles asynchronous operations. It allows Node.js to perform non-
blocking I/O operations, even though JavaScript is single-threaded, by offloading operations to the
system kernel whenever possible.
REPL stands for Read-Eval-Print-Loop. It is an interactive shell that processes Node.js expressions:
package.json contains metadata about a Node.js project (name, version, dependencies, scripts, etc.).
It is essential for managing project dependencies and scripts.
Buffers are temporary memory storage primarily used to handle binary data, such as when reading
or processing files or network packets in Node.js.
7. What is the use of __dirname and __filename in Node.js?
• process.nextTick(): Executes the callback immediately after the current operation, before
any I/O events.
• setImmediate(): Executes the callback on the next iteration of the Event Loop, after I/O
events.
A callback function is a function passed into another function as an argument, which is executed
after the parent function has completed.
Middleware functions in Express.js are functions that have access to the request, response, and next
middleware. They are used for tasks like authentication, logging, and error handling.
3 Marks Questions
• However, assigning a new object to exports doesn't affect module.exports unless they
reference the same object.
• Callbacks,
• Promises,
• async/await,
• The Event Loop mechanism, which ensures non-blocking execution.
The Cluster module allows Node.js to create child processes (workers) that share the same server
port. It enables multi-core CPU utilization, improving the performance of Node.js applications.
Streams are objects that let you read or write data continuously in chunks instead of loading it all at
once, improving performance. Example: Reading a file line-by-line using streams.
• Asynchronous functions allow the program to continue execution without waiting for the
operation to complete.
The EventEmitter class provides a way to handle events and event-driven programming by allowing
objects to emit named events and listen to them via callback functions.
Middleware functions in Express.js are used to modify req and res objects, execute code, and end
the request-response cycle or call the next middleware.
• createReadStream: Reads the file in small chunks (streaming) which is memory efficient.
10. How does Node.js handle child processes?
Node.js can spawn child processes using the child_process module (spawn, exec, fork methods) for
running system commands or handling parallel tasks.
4 Marks Questions
Node.js has:
2. Create a server.
The Event Loop constantly checks the call stack and the callback queue. If the stack is empty, it takes
the first event from the queue and pushes its callback into the stack for execution.
4. What are the different types of streams in Node.js, and how are they used?
• Use HTTPS.
10. What are the common performance bottlenecks in Node.js applications, and how can they be
addressed?
The HTTP module allows Node.js to create an HTTP server that can listen to server ports and send
responses to clients.
res.write('Hello World');
res.end();
});
CORS (Cross-Origin Resource Sharing) allows servers to specify who can access resources.
In Node.js, handle it using cors middleware:
app.use(cors());
body-parser parses incoming request bodies in a middleware, making the data available under
req.body.
emitter.addListener('greet', () => {
console.log('Hello, World!');
});
emitter.emit('greet');
17. Write a code for selecting all records from Player's table.
connection.connect();
console.log(results);
});
connection.end();
18. Write a program to use SQL SELECT query to show data from a table using Node.js and MySQL
database.
(Same as Q16)
20. Write a Program to define Module Circle.js which exports the functions area() and
circumference() and display the details on console.
Circle.js
Main File