Building a Clean CRUD Backend with Node.js and MongoDB Quiz

Explore foundational concepts for designing a modular, maintainable CRUD API using Node.js, Express, and MongoDB. This quiz highlights best practices for clean architecture, database integration, and efficient backend development.

  1. Setting Up the Project

    Which file type is commonly used to store sensitive configuration variables (such as database credentials) in a Node.js project to keep them separate from the codebase?

    1. config.js
    2. data.csv
    3. settings.json
    4. .env

    Explanation: .env is the standard for storing environment variables separately from code, promoting security and flexibility. config.js and settings.json may be used for configuration but are not suited for sensitive secrets. data.csv is a data format, not intended for configuration.

  2. Database Integration

    What is the primary purpose of using Mongoose in a Node.js and MongoDB API project?

    1. To provide an Object Data Modeling (ODM) layer
    2. To serve static frontend files
    3. To schedule background jobs
    4. To handle request validation

    Explanation: Mongoose is specifically designed to work as an ODM, mapping data between MongoDB and JavaScript objects. Serving static files and request validation are handled by other tools, while job scheduling is managed with libraries like node-cron.

  3. API Structure and Organization

    Why is it a best practice to separate controller logic from route definitions in a RESTful API codebase?

    1. It increases database indexing speed
    2. It reduces API response size
    3. It prevents users from sending duplicate requests
    4. It improves code maintainability and scalability as the project grows

    Explanation: Separating controllers from routes encourages a clear structure, making the codebase easier to navigate and extend. The other options do not result from this separation; response size, request duplication, and database indexing are unrelated.

  4. Developing and Testing the API

    Which tool is commonly used to automatically restart a Node.js server upon code changes during development?

    1. nodemon
    2. eslint
    3. webpack
    4. mongoose

    Explanation: nodemon monitors file changes and restarts the development server automatically. webpack is a bundler, mongoose is an ODM, and eslint is a linter. None of these others manage server restarts.

  5. Modular and Clean Code Principles

    What is a key benefit of using a modular folder structure, such as separating models, routes, and controllers, in a backend API project?

    1. Automatic generation of API documentation
    2. Faster database query execution
    3. Easier maintenance and future extension of the codebase
    4. Reduced memory usage at runtime

    Explanation: Organizing code into modules by responsibility simplifies updates and future growth. Faster queries and reduced memory require other optimizations, while documentation requires dedicated tools.