0% found this document useful (0 votes)
2 views5 pages

Nodejs

Uploaded by

tathavan.mng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Nodejs

Uploaded by

tathavan.mng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is Node.js?

- Answer: Node.js is a JavaScript runtime environment that allows us to run


JavaScript code on the server / outside of the browser. It’s built on Chrome’s
V8 engine and is used for building fast and scalable server-side applications.

2. How is Node.js different from JavaScript in the browser?


- Answer: In the browser, JavaScript is mainly used to interact with web
pages. In Node.js, JavaScript can handle backend tasks like database
operations, file handling, and server creation, which can’t be done in the
browser.

3. What is NPM?
- Answer: NPM stands for Node Package Manager. It’s a tool to install,
update, and manage packages (libraries) in Node.js, helping developers use
existing code to save time.

4. What is a module in Node.js?


- Answer: A module is a reusable piece of code in Node.js. It could be a
function, object, or class. You can use modules to organize your code into
smaller parts.

5. How do you export and import modules in Node.js?


- Answer: To export a module, you use `module.exports`. To import, use
`require()`.

6. What is `require()` in Node.js?


- Answer: `require()` is used to import modules into your code. You use it to
bring in libraries or other files with functions you want to use.

7. What is the purpose of `async` and `await` in Node.js?


- Answer: `async` and `await` are used for handling asynchronous operations
(like fetching data from a database) in a cleaner way. `async` makes a function
return a promise, and `await` waits for the promise to finish before moving to
the next line of code.

8. What is a callback function?


- Answer: A callback function is a function passed as an argument to another
function. It runs after the main function finishes. In Node.js, callbacks are
commonly used for tasks like reading files or handling network requests.
9. What is Express.js?
- Answer: Express.js is a framework for Node.js that makes building web
applications and APIs easier by providing tools to handle routes, requests, and
responses efficiently.

10. Explain middleware in Express.js.


- Answer: Middleware is a function that runs between receiving a request
and sending a response. It can be used for logging, authentication, error
handling, and more.

11. What is REST API?


- Answer: A REST API (Representational State Transfer) is a set of rules that
allow different software to communicate over the web. In Node.js, you use
REST APIs to create, read, update, and delete data using HTTP methods (like
GET, POST, PUT, DELETE).

12. Explain the event-driven architecture in Node.js.


- Answer: Node.js uses an event-driven architecture, meaning it waits for
events (like user requests) and responds. It allows multiple requests to be
processed without waiting for others to complete, making Node.js very fast.

13. What is the `package.json` file?


- Answer: `package.json` is a file in Node.js that stores information about the
project and its dependencies (libraries). It helps manage project configurations,
scripts, and installed packages easily.

14. How do you create a basic server in Node.js?


- Answer: You can create a server using the built-in `http` module.
- Example:

let http = require("http")

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

res.writeHead(200,'ok',{"content-type":"text/html"});
res.end(data)

})
server.listen(5000,(err)=>{
if(err) throw err;

console.log('server is running on port 5000')


})

15. What is the purpose of `process` in Node.js?


- Answer: The `process` object gives information about the current Node.js
process. It’s used to handle command-line arguments, environment variables,
and even exit the program if needed.

16. How does non-blocking I/O work in Node.js?


- Answer: Non-blocking I/O means that Node.js doesn’t wait for a task to
finish before moving to the next one. For example, if it’s reading a file, Node.js
can still handle other tasks while waiting for the file reading to complete.

17. What is an event loop in Node.js?


- Answer: The event loop is a mechanism that handles all asynchronous
operations in Node.js. It listens for events (like I/O operations) and executes
callbacks when they’re ready, allowing Node.js to be fast and handle many
tasks at once.

18. What is the difference between `readFileSync` and `readFile` in Node.js?


- Answer: `readFileSync` is synchronous, so it waits for the file to be read
completely before moving on. `readFile` is asynchronous and allows other code
to run while it reads the file.
- Example:

const fs = require('fs');
const data = fs.readFileSync('file.txt', 'utf8');
console.log(data);

fs.readFile('file.txt', 'utf8', (err, data) => {


if (err) throw err;
console.log(data);
});

19. What is the difference between `==` and `===` in JavaScript?


- Answer: `==` checks for value equality with type conversion (loose equality),
while `===` checks for both value and type equality (strict equality).
20. How do you handle errors in Node.js?
- Answer: You can handle errors in Node.js using `try-catch` blocks for
synchronous code, and error-first callbacks or `catch()` for asynchronous code
with promises.
- Example with a callback:

fs.writeFile("san.txt"," hello my name is santanu", (err)=>{

if(err) throw err


console.log('file created....')
})

21. What is JSON and how is it used in Node.js?


- Answer: JSON (JavaScript Object Notation) is a format for storing and
exchanging data. In Node.js, JSON is used to send data between the server and
client, and you can easily parse JSON with `JSON.parse()` and convert objects
to JSON with `JSON.stringify()`.

22. What is a promise in Node.js?


- Answer: A promise is an object representing the eventual completion or
failure of an asynchronous operation. Promises make it easier to work with
async code and avoid callback hell.

23. Explain the difference between `app.use()` and `app.get()` in Express.js.


- Answer: `app.use()` is for applying middleware or setting up routes,
whereas `app.get()` is specifically for handling GET requests to retrieve data.

24. What are streams in Node.js?


- Answer: Streams are objects that let you read or write data piece-by-piece,
making it memory-efficient for large files. Node.js has several types of streams:
readable, writable, duplex, and transform.

25. What are some use cases for Node.js?


- Answer: Node.js is widely used for building REST APIs, real-time chat
applications, microservices, streaming apps, and single-page applications due
to its fast performance and scalability.
26. What are events in Node.js?
- Answer: Events in Node.js are actions or occurrences, like receiving a
request or completing a file read operation. Node.js can listen to and respond
to these events, which helps manage asynchronous tasks without blocking the
main thread.

27. What is the EventEmitter class in Node.js?


- Answer: The `EventEmitter` class is part of Node.js’s `events` module and
allows objects to emit named events. Other parts of the code can listen for
these events and respond when they’re emitted.

28. How do you use the EventEmitter in Node.js?


- Answer: First, import the `events` module, create an `EventEmitter` object,
and then set up listeners using `.on()` and emit events with `.emit()`.
- Example:
const EventEmitter = require('events');

const eventEmitter = new EventEmitter();

// Listener
eventEmitter.on('greet', () => {
console.log('Hello there!');
});

// Emit the event


eventEmitter.emit('greet');

29. What is the purpose of `.on()` and `.emit()` methods in Node.js?


- Answer: `.on()` is used to attach a listener (a function that runs when an
event happens) to an event. `.emit()` is used to trigger an event, running all
listeners attached to that event.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy