Node.js REST API
- 26Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- 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?
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
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.
What CRUD operations will the Node.js API support?
Create, Read, Update, and Delete (CRUD).
What file type will be used for data storage in this vanilla Node.js API?
A JSON file (
products.json).What core Node.js module is essential for building this API without frameworks?
The
httpmodule.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.urlandrequest.methodconditionals. Express.js provides directapp.get(),app.post(), etc., methods.What is the purpose of the
http.createServer()method?It kickstarts the HTTP server and takes a function that receives
requestandresponseobjects for handling incoming requests.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.How do you set the HTTP status code and content type for a response?
Using
res.writeHead(statusCode, { 'Content-Type': 'value' }).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 withres.end().What are common HTTP status codes and their meanings?
200 (OK), 201 (Created), 3xx (Redirects), 400 (Bad Request), 404 (Not Found), 5xx (Server Errors).
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.What is the purpose of the
package.jsonfile?It defines project metadata, scripts, and dependencies.
How is
nodemonused in this project?nodemonis used as a development dependency to automatically restart the server whenever changes are made to the code, avoiding manual restarts.What command is used to initialize a
package.jsonfile?npm initornpm init -y(to skip interactive questions).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.
What is the role of the
product model.jsfile?It handles all data-related logic for products, such as finding, creating, updating, and deleting products from the JSON file.
What is the role of the
product controller.jsfile?It contains functions that handle specific routes and HTTP methods, process requests, interact with the product model, and send appropriate responses.
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.
How do you access URL parameters like an ID in a vanilla Node.js API?
By manually parsing
request.urlusing string manipulation methods likesplit()to extract the ID from the path.What is the regular expression used to match a specific product ID in the URL?
^/api/products/[0-9]+$How is the body data from a POST or PUT request handled in a vanilla Node.js API?
The
requestobject emitsdataevents (for chunks) and anendevent. Chunks are appended to a buffer and converted to a string, then parsed withJSON.parse()on theendevent.What utility function is created to simplify getting POST/PUT data?
getPostDatawhich takes therequestobject and returns a promise that resolves with the parsed body data.How are unique IDs generated for new products in this API?
By using the
uuidpackage, specificallyuuid.v4()to generate version 4 UUIDs.What Node.js module is used to read from and write to the
products.jsonfile?The
fs(File System) module.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 usingfs.writeFile.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.

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