100% found this document useful (1 vote)
67 views4 pages

Backend Interview Questions

Node.js is a JavaScript runtime environment that allows JavaScript to run outside of the browser. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Common Node.js modules include http for creating servers, fs for file system operations, and events for event-driven programming. Developers can use modules, require(), and module.exports to reuse code and build scalable network applications.

Uploaded by

tarun chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
100% found this document useful (1 vote)
67 views4 pages

Backend Interview Questions

Node.js is a JavaScript runtime environment that allows JavaScript to run outside of the browser. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Common Node.js modules include http for creating servers, fs for file system operations, and events for event-driven programming. Developers can use modules, require(), and module.exports to reuse code and build scalable network applications.

Uploaded by

tarun chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

1. **What is Node?

**
Node.js is an open-source, cross-platform JavaScript runtime environment that
allows developers to execute JavaScript code outside of a web browser. It is built
on Chrome's V8 JavaScript engine and provides an event-driven, non-blocking I/O
model that makes it lightweight and efficient.

2. **Is Node a single thread or multi-thread?**


Node.js is single-threaded, but it uses an event loop and asynchronous
programming to handle concurrent operations efficiently. It can handle many
concurrent connections without the need for creating threads for each connection.

3. **What is I/O?**
I/O stands for Input/Output. In the context of Node.js, I/O refers to reading
from or writing to external resources like files, network sockets, or databases.

4. **How many types of API functions are available in Node.js?**


There are four types of API functions available in Node.js:
- Asynchronous, non-blocking functions that use callbacks.
- Asynchronous functions that return Promises (introduced in newer versions of
Node.js).
- Synchronous functions that block the execution until the operation is
complete.
- Asynchronous functions that use async/await syntax (introduced in newer
versions of Node.js).

5. **What are modules in Node?**


Modules in Node.js are reusable blocks of code that encapsulate related
functionality. They allow developers to organize their code into separate files and
provide a way to share code between different parts of an application.

6. **What are the common modules in Node?**


Common modules in Node.js include:
- `http`: Provides functionality for creating HTTP servers and clients.
- `fs`: Provides file system-related operations.
- `path`: Handles file path operations.
- `events`: Enables event-driven programming.
- `util`: Offers utility functions for various purposes.

7. **What is `require()` used for in Node.js?**


`require()` is a function in Node.js used to import modules. It allows you to
include external modules or files into your application and use their
functionality.

8. **What is the use of `module.exports` in Node.js?**


`module.exports` is a special object in Node.js that is used to expose
functionality from a module. By assigning values or functions to `module.exports`,
you can make them available for other modules to use when they `require()` the
module.

9. **Which module is used to create a server in Node.js?**


The `http` module is used to create an HTTP server in Node.js. It provides the
`createServer()` method to create an instance of an HTTP server that can listen for
incoming requests and send responses.

10. **List a few modules in Node.js.**


Some commonly used modules in Node.js are:
- `http`: For creating HTTP servers and clients.
- `fs`: For file system operations.
- `path`: For handling file paths.
- `events`: For working with events.
- `util`: For utility functions.
- `express`: For building web applications.
- `mongoose`: For working with MongoDB.

11. **What is the use of the `http` module?**


The `http` module in Node.js provides functionality for creating HTTP servers
and clients. It allows you to handle incoming HTTP requests, send HTTP responses,
and perform various operations related to the HTTP protocol.

12. **How do you read and write files in Node.js?**


In Node.js, you can read and write files using the `fs` module. The `fs` module
provides methods such as `readFile()`, `writeFile()`, `appendFile()`, etc., which
allow you to

perform file-related operations like reading, writing, or appending data to files.

13. **What is the event loop in Node.js?**


The event loop is a fundamental concept in Node.js that allows it to handle
concurrent operations efficiently. It is responsible for managing asynchronous
callbacks and event-driven programming. The event loop continuously checks for
pending events, executes the associated callbacks, and returns the results when
they are ready.

14. **What is an asynchronous API?**


An asynchronous API in Node.js allows you to perform operations without
blocking the execution of the program. Instead of waiting for an operation to
complete, it initiates the operation and provides a callback or a Promise to handle
the result when it becomes available. This enables non-blocking I/O and helps
maximize the efficiency of the program.

15. **Is it possible to access the DOM in Node.js?**


No, the DOM (Document Object Model) is a web browser API, and Node.js is a
server-side JavaScript runtime environment. Node.js does not provide direct access
to the DOM. However, there are libraries like `jsdom` that can simulate a DOM
environment within Node.js for certain use cases.

16. **What is event-driven programming in Node.js?**


Event-driven programming is a programming paradigm in which the flow of the
program is determined by events and how they are handled. In Node.js, event-driven
programming is facilitated through the use of event emitters and listeners.
Asynchronous operations can emit events when they complete, and callbacks or event
listeners can be registered to handle those events.

17. **What is the difference between events and callbacks in Node.js?**


In Node.js, events and callbacks are related but serve different purposes:
- Events: Events are a mechanism for signaling and handling asynchronous
actions or occurrences. They can be emitted by objects and listened to by event
listeners. Events allow for loose coupling between different parts of an
application.
- Callbacks: Callbacks are functions that are passed as arguments to other
functions. They are used to handle the result or error of an asynchronous
operation. Callbacks are invoked when the operation completes, allowing the program
to continue its execution.

18. **How to create a simple server in Node.js that returns "Hello, World"?**
Here's an example of creating a simple server in Node.js that returns "Hello,
World" for all incoming requests:
```javascript
const http = require('http');

const server = http.createServer((req, res) => {


res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World');
});

server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
```

19. **Is Node.js a framework?**


No, Node.js is not a framework. It is a runtime or environment that allows the
execution of JavaScript code outside of a web browser. However, there are
frameworks built on top of Node.js, such as Express.js, that provide additional
features and abstractions to simplify web development.

20. **What is npm?**


npm (Node Package Manager) is the default package manager for Node.js. It
allows developers to discover, install, and manage packages and dependencies for
their Node.js projects. npm provides a vast ecosystem of open-source packages that
can be easily integrated into Node.js applications.

21. **What tools can be used to assure a consistent style in Node.js?**


Tools like ESLint and Prettier can be used to ensure consistent code style in
Node.js projects. ESLint is a static code analysis tool that can enforce coding
standards and detect potential issues, while Prettier is a code formatter that
automatically formats code to adhere to a consistent style guide

22. **What is Express?**


Express is a popular web application framework for Node.js. It provides a set
of features and abstractions to simplify the development of web applications.
Express offers routing, middleware support, template engines, and other utilities
to handle HTTP requests and responses in a structured manner.

23. **What is Express middleware?**


Express middleware are functions that are executed in the request-response
cycle of an Express application. They can intercept and modify incoming requests or
outgoing responses. Middleware functions can perform tasks such as authentication,
logging, data parsing, error handling, and more.

24. **What is Mongoose?**


Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It
provides a higher-level abstraction for interacting with MongoDB, allowing you to
define schemas, models, and perform database operations in a more intuitive and
structured way.

25. **How do you use Mongoose to connect Express and MongoDB?**


To use Mongoose with Express and MongoDB, you would typically follow these
steps:
- Install Mongoose: Use npm to install the Mongoose package in your project.
- Connect to MongoDB: Use the `mongoose.connect()` method to establish a
connection to your MongoDB database.
- Define models: Create Mongoose models that represent your data structure and
define schema validations.
- Perform CRUD operations: Use the Mongoose models to perform database
operations like creating, reading, updating, and deleting documents in MongoDB.

Here's a simplified example of connecting Express and MongoDB using Mongoose:

```javascript
const express = require('express');
const mongoose = require('mongoose');

// Create Express app


const app = express();

// Connect to MongoDB
mongoose.connect('mongodb://localhost/mydatabase', {
useNewUrlParser: true,
useUnifiedTopology: true,
});

// Define a Mongoose schema and model


const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model('User', userSchema);

// Example route
app.get('/users', async (req, res) => {
const users = await User.find();
res.json(users);
});

// Start the server


app.listen(3000, () => {
console.log('Server is listening on port 3000');
});
```

Please let me know if you have any further questions!

You might also like