Explore essential backend concepts by building fast and functional To-Do and Notes REST APIs using Node.js and Express. Understand CRUD operations, project setup, and best practices for API development.
Which command initializes a new Node.js project and creates a package.json file in your project directory?
Explanation: The command 'npm init -y' initializes a Node.js project and generates a package.json file with default values. 'npn start' is incorrect due to a typo and does not initialize a project. 'node install' is not a valid npm or Node.js command. 'npm create project' is also not a recognized npm command for initialization.
Why is the Express middleware 'express.json()' added before defining API routes?
Explanation: The 'express.json()' middleware parses JSON-formatted request bodies, which allows API endpoints to access data sent via POST or PUT. Connecting to a database and serving static files require different middleware or configurations. Enabling HTTPS is unrelated to parsing request bodies.
What happens if a client attempts to create a new to-do task without providing a title in the request body?
Explanation: The API is designed to check for the presence of a 'title' and respond with HTTP 400 and an error message if it's missing. Creating a task with a default or random title is not implemented. Server crashes are not expected in this scenario, as input validation handles the error properly.
Which HTTP method and endpoint combination is used to update an existing to-do's title or completion status?
Explanation: 'PUT /todos/:id' is the standard RESTful approach for updating a resource. 'POST /todos' is used for creating new tasks, 'GET /todos' retrieves all tasks, and 'DELETE /todos/:id' removes a specific task rather than updating it.
When creating a note, which fields must be included in the request body for a successful response?
Explanation: Both 'title' and 'content' are required fields when creating a new note; missing either results in an error. 'ID' and 'timestamp' are generated automatically, not required from the client. 'Completed', 'user', 'priority', and 'label' are not part of the required note fields.