Building Your First Backend: To-Do & Notes APIs with Node.js and Express Quiz

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.

  1. Project Initialization and Structure

    Which command initializes a new Node.js project and creates a package.json file in your project directory?

    1. node install
    2. npm create project
    3. npm init -y
    4. npn start

    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.

  2. Express Middleware Usage

    Why is the Express middleware 'express.json()' added before defining API routes?

    1. To enable HTTPS
    2. To parse incoming JSON request bodies
    3. To connect to a database
    4. To serve static files

    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.

  3. Creating and Validating API Endpoints

    What happens if a client attempts to create a new to-do task without providing a title in the request body?

    1. A random title is assigned automatically
    2. The server crashes
    3. The server responds with a 400 status and an error
    4. The task is created with a default title

    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.

  4. CRUD Operations in To-Do API

    Which HTTP method and endpoint combination is used to update an existing to-do's title or completion status?

    1. POST /todos
    2. DELETE /todos/:id
    3. PUT /todos/:id
    4. GET /todos

    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.

  5. Notes API Data Handling

    When creating a note, which fields must be included in the request body for a successful response?

    1. Title and content
    2. ID and timestamp
    3. Priority and label
    4. Completed and user

    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.