1. Home
  2. Explore
  3. Computer Science
  4. Node.js REST API

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Node.js REST API

  • 26Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Project Overview
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is the primary purpose of this project regarding Node.js APIs?

Tap to flipClick to flipspace

To create a vanilla Node.js REST API with full CRUD functionality without using frameworks like Express, relying only on the built-in HTTP module.

Or swipe the card

26 cards

  1. What is the primary purpose of this project regarding Node.js APIs?

    To create a vanilla Node.js REST API with full CRUD functionality without using frameworks like Express, relying only on the built-in HTTP module.

  2. What CRUD operations will the Node.js API support?

    Create, Read, Update, and Delete (CRUD).

  3. What file type will be used for data storage in this vanilla Node.js API?

    A JSON file (products.json).

  4. What core Node.js module is essential for building this API without frameworks?

    The http module.

  5. How does handling routes and HTTP methods differ in a vanilla Node.js API compared to Express.js?

    In a vanilla Node.js API, routes and methods must be handled manually using request.url and request.method conditionals. Express.js provides direct app.get(), app.post(), etc., methods.

  6. What is the purpose of the http.createServer() method?

    It kickstarts the HTTP server and takes a function that receives request and response objects for handling incoming requests.

  7. How is the server configured to listen for incoming requests?

    By calling server.listen() and providing a port number, often checking for an environment variable (process.env.PORT) or defaulting to a specific port like 5000.

  8. How do you set the HTTP status code and content type for a response?

    Using res.writeHead(statusCode, { 'Content-Type': 'value' }).

  9. How do you send JSON data in a vanilla Node.js HTTP response?

    The JavaScript object must be converted to a JSON string using JSON.stringify() before being sent with res.end().

  10. What are common HTTP status codes and their meanings?

    200 (OK), 201 (Created), 3xx (Redirects), 400 (Bad Request), 404 (Not Found), 5xx (Server Errors).

  11. How can you prevent a Node.js server from hanging if no response is sent?

    Always ensure res.end() is called to terminate the response cycle, even if sending an empty response or an error.

  12. What is the purpose of the package.json file?

    It defines project metadata, scripts, and dependencies.

  13. How is nodemon used in this project?

    nodemon is used as a development dependency to automatically restart the server whenever changes are made to the code, avoiding manual restarts.

  14. What command is used to initialize a package.json file?

    npm init or npm init -y (to skip interactive questions).

  15. Explain the Model-View-Controller (MVC) design pattern in the context of this API.

    Models handle data (e.g., JSON file interactions), Views are for presentation (not used in this API), and Controllers handle requests, responses, and interact with models.

  16. What is the role of the product model.js file?

    It handles all data-related logic for products, such as finding, creating, updating, and deleting products from the JSON file.

  17. What is the role of the product controller.js file?

    It contains functions that handle specific routes and HTTP methods, process requests, interact with the product model, and send appropriate responses.

  18. Why do model functions return promises in this API?

    Because data operations (like reading/writing files or database interactions) are typically asynchronous, and promises provide a structured way to handle their eventual completion or failure.

  19. How do you access URL parameters like an ID in a vanilla Node.js API?

    By manually parsing request.url using string manipulation methods like split() to extract the ID from the path.

  20. What is the regular expression used to match a specific product ID in the URL?

    ^/api/products/[0-9]+$

  21. How is the body data from a POST or PUT request handled in a vanilla Node.js API?

    The request object emits data events (for chunks) and an end event. Chunks are appended to a buffer and converted to a string, then parsed with JSON.parse() on the end event.

  22. What utility function is created to simplify getting POST/PUT data?

    getPostData which takes the request object and returns a promise that resolves with the parsed body data.

  23. How are unique IDs generated for new products in this API?

    By using the uuid package, specifically uuid.v4() to generate version 4 UUIDs.

  24. What Node.js module is used to read from and write to the products.json file?

    The fs (File System) module.

  25. What utility function is created to write data to a file?

    writeDataToFile, which takes a filename and content, stringifies the content, and writes it to the specified file using fs.writeFile.

  26. How is partial update functionality achieved for products?

    When updating, each field (title, description, price) is set to either the new value from the request body OR its existing value from the product data, allowing optional field updates.

Card 1 of 26
Card 1 of 26

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent