npm_1

 Absolutely πŸ‘

Below are viva / exam questions with clear, concise model answers. These are written in exam-safe language, easy to memorize, and strong enough to score full marks.


πŸ”Ή Section A: Basic Viva Questions (with Model Answers)

1. What is npm and why is it used in Node.js?

Answer:
npm (Node Package Manager) is used to install, manage, and share external libraries required for Node.js applications.


2. What does npm init -y do?

Answer:
It initializes a Node.js project and automatically creates a package.json file with default values.


3. Which file is created when npm init -y is executed?

Answer:
A package.json file is created.


4. What information is stored in the package.json file?

Answer:
It stores project metadata such as name, version, scripts, dependencies, and entry file.


5. What is the purpose of the dependencies section in package.json?

Answer:
It lists all external packages required for the application to run.


6. Why do we use npm install?

Answer:
To download and install required packages into the node_modules folder.


7. What happens if node_modules folder is deleted?

Answer:
The application will stop working until dependencies are reinstalled using npm install.


8. Why should node_modules not be uploaded to GitHub?

Answer:
Because it is large and can be recreated using package.json.


πŸ”Ή Section B: Package-Specific Questions

Express

9. What is Express.js?

Answer:
Express.js is a lightweight Node.js framework used to build web applications and RESTful APIs.


10. Why is Express preferred over plain Node.js?

Answer:
Express simplifies routing, middleware handling, and request/response management.


11. How does Express help in building RESTful APIs?

Answer:
It provides HTTP methods like GET, POST, PUT, and DELETE with simple route definitions.


12. What is app.get() used for?

Answer:
It handles HTTP GET requests.


MySQL2

13. Why is mysql2 used instead of mysql?

Answer:
mysql2 is faster and supports modern features like Promises.


14. How does mysql2 help Node.js communicate with MySQL?

Answer:
It provides functions to connect, query, and retrieve data from MySQL databases.


15. What is a database connection pool?

Answer:
It maintains multiple reusable database connections to improve performance.


16. What happens if the database connection fails?

Answer:
The API returns an error and database operations cannot be performed.


CORS

17. What is CORS?

Answer:
CORS (Cross-Origin Resource Sharing) allows requests between different domains.


18. Why is CORS required?

Answer:
To allow frontend and backend running on different ports to communicate.


19. What error occurs if CORS is not enabled?

Answer:
A CORS policy error appears in the browser console.


20. How does cors() middleware help?

Answer:
It allows cross-origin requests by adding required headers.


Body-Parser

21. What is body-parser?

Answer:
body-parser extracts request body data and makes it available in req.body.


22. Why is body-parser required for POST and PUT requests?

Answer:
To read JSON or form data sent by the client.


23. What happens if body-parser is not used?

Answer:
req.body will be undefined.


24. What does bodyParser.json() do?

Answer:
It parses incoming JSON request bodies.


πŸ”Ή Section C: Applied / Scenario-Based

25. Explain express, mysql2, cors, and body-parser.

Answer:

  • Express: Builds APIs

  • mysql2: Connects MySQL database

  • cors: Enables cross-origin requests

  • body-parser: Parses request data


26. What changes occur in package.json after installing packages?

Answer:
Installed packages appear under the dependencies section.


27. How does Express use middleware?

Answer:
Middleware processes requests before reaching routes.


28. How does frontend communicate with backend?

Answer:
Using HTTP requests (fetch) to Express REST APIs connected to MySQL.


29. Why use npm init -y?

Answer:
It saves time by skipping manual inputs.


30. How is POST data processed using body-parser?

Answer:
body-parser reads request data and stores it in req.body.


πŸ”Ή Section D: Error Handling

31. Error if Express is not installed?

Answer:
Module not found error.


32. What if database credentials are wrong?

Answer:
Connection error is thrown.


33. How to verify installed packages?

Answer:
Using npm list.


34. What does npm list do?

Answer:
Displays installed dependencies.


35. How to fix dependency issues?

Answer:
Delete node_modules and run npm install.


πŸ”Ή Section E: Code Understanding

36. Why use app.use(express.json())?

Answer:
To parse JSON request bodies.


37. What does app.use(cors()) do?

Answer:
Enables cross-origin requests.


38. Why require mysql2?

Answer:
To interact with MySQL database.


39. What is middleware?

Answer:
Functions that execute before route handlers.


40. How does package.json help deployment?

Answer:
It allows recreating the environment using npm install.


πŸ”Ή Bonus

41. dependencies vs devDependencies?

Answer:
Dependencies are required at runtime; devDependencies are for development.


42. Why Node.js for REST APIs?

Answer:
It is fast, scalable, and non-blocking.


43. What happens when npm install runs?

Answer:
Downloads dependencies into node_modules.


44. How does Node.js handle multiple requests?

Answer:
Using event-driven, non-blocking architecture.


45. Why is Express lightweight?

Answer:
It provides only essential features.


If you want, I can:

  • πŸŽ“ Turn this into a viva cheat sheet

  • 🧠 Convert into MCQs

  • πŸ“„ Export as PDF

  • ✍️ Align answers exactly to your marking rubric

Just tell me πŸ‘

Comments

Popular posts from this blog

Python_While_Loop

Python_Lists_Loops

clinical_app