0% found this document useful (0 votes)
206 views12 pages

Node Js Short Notes

Node js

Uploaded by

tempmailforamit
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)
206 views12 pages

Node Js Short Notes

Node js

Uploaded by

tempmailforamit
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

NODE.

js SHORT NOTES
1. Introduction to [Link]
- What is [Link]?: [Link] is a JavaScript runtime built on Chrome's V8
JavaScript engine. It allows developers to run JavaScript on the server side.
- Non-blocking I/O: [Link] uses asynchronous, event-driven architecture, making
it efficient for I/O-heavy tasks.
- Single-threaded: Although [Link] runs on a single thread, it handles multiple
requests concurrently via an event loop.

2. Modules
- Built-in modules: [Link] provides various built-in modules like `fs` (File System),
`http` (for creating servers), `path`, etc.
- CommonJS Modules: The module system in [Link] uses `require()` to import
and `[Link]` to export.
- npm (Node Package Manager): Tool for installing and managing third-party
packages.

3. File System (fs) Module


- Provides methods to interact with the file system, such as reading, writing,
updating, and deleting files.
- Operations:
- `[Link]()`
- `[Link]()`
- `[Link]()`
- `[Link]()`
- `[Link]()`

4. HTTP Module
- Used to create a server in [Link].
- Example:
```js
const http = require('http');
const server = [Link]((req, res) => {
[Link]('Hello World!');
[Link]();
});
[Link](3000);
```
- Request and Response Objects: Handle incoming HTTP requests and return
appropriate responses.

5. [Link] Framework
- What is Express?: A minimalistic web framework for [Link] that simplifies
server-side code.
- Routing: Use `[Link]()`, `[Link]()`, etc., to define routes.
- Middleware: Functions that process requests before reaching the endpoint.
Example: `[Link]()`.
- Handling Forms and JSON Data: Parse incoming requests using middleware like
`body-parser`.

6. Event-driven Architecture
- Events and Event Emitter: [Link] uses an event-driven architecture where
certain functions (like file reads) emit events.
- EventEmitter Class: Events are handled using instances of the `EventEmitter`
class.
```js
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
[Link]('event', () => { [Link]('An event occurred!'); });
[Link]('event');
```

7. Asynchronous Programming
- Callbacks: Functions passed as arguments to other functions, executed once an
async task is completed.
- Promises: An alternative to callbacks, providing a cleaner way to handle async
operations.
- Async/Await: Syntactic sugar over promises, making asynchronous code look
synchronous.

8. Streams
- What are Streams?: Streams are objects that allow reading or writing data
continuously.
- Types of Streams:
- Readable: `[Link]()`
- Writable: `[Link]()`
- Duplex: Both readable and writable.
- Transform: Modifies the data as it is being read or written.
- Useful for handling large files.

9. Buffer
- What is Buffer?: Temporary storage for binary data, mainly used with streams.
- Buffer Class: Used to manipulate binary data in [Link].

10. Working with Databases


- MongoDB: NoSQL database, often used with [Link].
- Mongoose: ODM (Object Data Modeling) library for MongoDB, providing
schema-based data modeling.
- SQL Databases: [Link] can interact with SQL databases like MySQL,
PostgreSQL using libraries like `sequelize`, `pg`, `mysql`.

11. Error Handling


- Try-Catch: Handle synchronous errors.
- Error-first Callbacks: [Link] follows an error-first callback pattern, where the first
argument of the callback is an error object.
- Promise Error Handling: Use `.catch()` for errors in promises.
- Async/Await Error Handling: Use `try-catch` blocks to catch errors with async
functions.

12. Middleware in Express


- What is Middleware?: Functions that execute during the lifecycle of a request to
the server.
- Types of Middleware:
- Application-level: Bound to an instance of `express()`.
- Router-level: Bound to an instance of `[Link]()`.
- Error-handling Middleware: Takes four arguments `(err, req, res, next)` to
handle errors.

13. API Development


- RESTful APIs: [Link] is commonly used to create RESTful services.
- Routing in Express: Define routes for different HTTP methods (GET, POST,
PUT, DELETE).
- Handling JSON Data: Using `[Link]()` middleware to parse JSON
requests.

14. Security in [Link]


- [Link]: Middleware that adds security-related HTTP headers.
- Rate Limiting: Control the rate of requests using tools like `express-rate-limit`.
- Input Validation and Sanitization: Use libraries like `validator` to prevent SQL
injection, XSS, etc.

15. Websockets
- What is WebSocket?: A protocol for two-way communication between the client
and server over a single, long-lived connection.
- [Link]: Popular library for implementing WebSocket communication in
[Link].
16. Testing in [Link]
- Mocha: A testing framework for [Link].
- Chai: An assertion library used with Mocha for testing.
- Jest: Another popular testing framework, often used for both backend and
frontend.

17. Deployment
- PM2: A process manager to keep [Link] applications running.
- Docker: Used to containerize [Link] applications.
- Hosting Platforms: Deploy [Link] applications on services like Heroku, AWS,
Azure, and DigitalOcean.

18. Version Control and Managing Multiple Node Versions


- nvm (Node Version Manager): Tool for installing and managing multiple versions
of [Link].

QUESTION AND ANSWERS

Basic [Link] Questions:

1. What is [Link]?
- [Link] is a JavaScript runtime built on Chrome's V8 engine, allowing JavaScript
to run server-side.

2. Is [Link] single-threaded or multi-threaded?


- [Link] is single-threaded with an event-driven, non-blocking architecture.

3. What is npm?
- npm (Node Package Manager) is a package manager for JavaScript, providing
access to thousands of packages for use in [Link] projects.
4. What is the global object in [Link]?
- `global` is the global object in [Link], similar to `window` in a browser
environment.

5. What is the purpose of `require()` in [Link]?


- `require()` is used to load modules (built-in or user-defined) in [Link].

6. How do you create a server in [Link]?


```js
const http = require('http');
const server = [Link]((req, res) => {
[Link]('Hello World');
});
[Link](3000);
```

7. What is a callback function?


- A callback is a function passed as an argument to another function, which is
executed after an operation completes.

8. What is the event loop in [Link]?


- The event loop is the mechanism that allows [Link] to perform non-blocking I/O
operations despite being single-threaded by offloading operations to the system.

9. What is the role of the `fs` module in [Link]?


- The `fs` (File System) module allows [Link] to interact with the file system to
read, write, update, or delete files.

10. What is the difference between `readFileSync` and `readFile`?


- `readFileSync` is a synchronous method that blocks the execution until the file is
read, while `readFile` is asynchronous.
Intermediate [Link] Questions:

11. What are streams in [Link]?


- Streams are objects that let you read or write data continuously. They are useful
for handling large amounts of data.

12. What are the types of streams in [Link]?


- Readable, Writable, Duplex, and Transform streams.

13. What is middleware in [Link]?


- Middleware are functions that execute during the lifecycle of a request to the
server and can modify the request or response.

14. How does [Link] handle concurrency?


- [Link] uses an event loop and a single thread, handling concurrency via non-
blocking I/O and callbacks.

15. What is `[Link]`?


- `[Link]` is used to export variables, objects, or functions from a
module, making them available to other modules via `require()`.

16. What is a promise in [Link]?


- A promise is an object representing the eventual completion or failure of an
asynchronous operation.

17. What is async/await in [Link]?


- `async` and `await` are syntactic sugar for handling promises, making
asynchronous code look synchronous.

18. What is `[Link]()`?


- `[Link]()` schedules a callback function to be invoked in the next
iteration of the event loop, before any I/O tasks.

19. How do you handle errors in [Link]?


- Errors can be handled using `try-catch`, error-first callbacks, or `.catch()` for
promises.

20. What is the difference between `[Link]()` and `[Link]()` in Express?


- `[Link]()` can send any type of response (HTML, JSON, plain text), while
`[Link]()` is used to send a JSON response.

Advanced [Link] Questions:

21. What is the `cluster` module in [Link]?


- The `cluster` module enables the creation of child processes (workers) that
share the same server port to handle multiple requests concurrently.

22. What is the difference between [Link] and [Link]?


- `[Link]` contains environment variables, while `[Link]` contains the
command-line arguments passed when starting a [Link] process.

23. What is the purpose of the `buffer` in [Link]?


- `Buffer` is used to handle binary data in [Link], especially when working with
streams or files.

24. How can you make a module globally accessible in [Link]?


- To make a module globally accessible, you can install it globally using `npm
install -g` or modify the `global` object.

25. What are worker threads in [Link]?


- Worker threads enable running JavaScript in parallel on multiple threads, useful
for CPU-intensive tasks.
26. What is the difference between `setImmediate()` and `setTimeout()`?
- `setImmediate()` executes a callback on the next iteration of the event loop,
while `setTimeout()` schedules execution after a minimum delay.

27. How do you connect [Link] to a MongoDB database?


- Use the MongoDB [Link] driver or an ODM like Mongoose.
```js
const mongoose = require('mongoose');
[Link]('mongodb://localhost/testdb');
```

28. What is the purpose of `[Link]()` in Express?


- `[Link]()` is used to apply middleware functions to an Express application.

29. How does [Link] handle child processes?


- The `child_process` module in [Link] allows you to spawn child processes,
enabling parallel execution of tasks.

30. What is [Link]?


- [Link] is an Express middleware that helps secure apps by setting various
HTTP headers.

[Link] Security Questions:

31. What is CORS?


- CORS (Cross-Origin Resource Sharing) is a security feature that allows or
restricts resources on a web page to be requested from another domain.

32. How can you prevent SQL injection in [Link]?


- Use parameterized queries or an ORM (like Sequelize or Mongoose) to prevent
SQL injection.
33. What is CSRF?
- Cross-Site Request Forgery (CSRF) is an attack that forces a user to execute
unwanted actions on a web application.

34. How do you prevent CSRF attacks in [Link]?


- Use CSRF tokens (middleware like `csurf`) to verify requests’ authenticity.

35. How can you secure sensitive data in [Link]?


- Encrypt sensitive data using libraries like `crypto` and always store credentials in
environment variables.

36. What is rate limiting, and how can you implement it in [Link]?
- Rate limiting restricts the number of requests a client can make in a specific time
period, and it can be implemented using the `express-rate-limit` package.

37. What is input validation, and why is it important in [Link]?


- Input validation ensures that user data is correct and safe. It helps prevent
common attacks like SQL injection and XSS (Cross-Site Scripting).

38. What is XSS, and how can you prevent it?


- XSS (Cross-Site Scripting) is an attack that injects malicious scripts into web
pages. You can prevent it by validating and sanitizing input, escaping HTML
characters, and using libraries like `xss-clean`.

39. How do you encrypt passwords in [Link]?


- Use hashing algorithms like bcrypt to securely store passwords.
```js
const bcrypt = require('bcrypt');
[Link]('password', saltRounds, function(err, hash) { });
```
40. How can you handle user authentication in [Link]?
- Use JWT (JSON Web Token) or [Link] for handling authentication.

[Link] Ecosystem Questions:

41. What is the role of Mongoose in a [Link] application?


- Mongoose is an ODM (Object Data Modeling) library for MongoDB, providing a
schema-based solution to model your application data.

42. What is the difference between [Link] and Deno?


- Deno is a secure runtime for JavaScript and TypeScript, with features like built-
in TypeScript support, while [Link] focuses on JavaScript with a large ecosystem.

43. What is [Link]?


- [Link] is a library that enables real-time, bidirectional communication
between clients and servers over WebSocket or polling.

44. What is Sequelize?


- Sequelize is a promise-based [Link] ORM for SQL databases like MySQL,
PostgreSQL, SQLite, and others.

45. What is Nodemon?


- Nodemon is a tool that automatically restarts a [Link] server when file changes
are detected.

46. How do you test a [Link] application?


- Use testing libraries like Mocha, Chai, or Jest to write unit and integration tests.
47. What is the difference between synchronous and asynchronous functions in
[Link]?
- Synchronous functions block the execution of subsequent code until they
complete, while asynchronous functions do not block and use callbacks, promises,
or async/await to manage execution.

48. How can you debug a [Link] application?


- Use built-in debugging tools like `[Link]()`, or more advanced tools like the
[Link] Inspector and `node --inspect`.

49. What is clustering in [Link]?


- Clustering allows you to take advantage of multi-core systems by running
multiple instances of a [Link] application, each handling incoming requests
concurrently.

50. What are the benefits of using [Link]?


- Fast execution (thanks to V8 engine), non-blocking I/O for scalability, large
community and ecosystem (npm), and JavaScript support on both client and server.

You might also like