Building a REST API with Node.js and Express.js: A Step-by-Step Guide Quiz

Learn key principles and practical steps in creating a REST API using Node.js and Express.js, from environment setup to handling requests. This quiz covers essential concepts like routes, HTTP verbs, and CRUD operations for beginners in backend development.

  1. REST API Concepts

    Which principle ensures that each client request to a REST API contains all information needed for the server to process it without relying on stored context?

    1. Synchronous Execution
    2. Stateful Connection
    3. Strict Typing
    4. Statelessness

    Explanation: Statelessness requires each request to be self-contained, which is a core REST concept. Stateful Connection is the opposite and not a REST principle. Strict Typing relates to programming languages, not REST. Synchronous Execution pertains to how code runs but isn't directly related to REST architecture.

  2. Environment Setup

    What is the main purpose of running 'npm init -y' when starting a Node.js REST API project?

    1. To install the Express.js framework automatically
    2. To create a database for storing API data
    3. To generate a default package.json file with basic configurations
    4. To start the local development server

    Explanation: 'npm init -y' quickly creates a package.json file, initializing the project with default settings. It does not install Express.js, start a server, or set up a database—these tasks require separate commands.

  3. Express Server Initialization

    After setting up an Express.js server on port 3000, what output appears in the terminal when it starts successfully?

    1. Error: Missing dependencies
    2. Server started at port 4000
    3. API endpoint not found
    4. Server running on http://localhost:3000

    Explanation: A successful start on port 3000 displays 'Server running on http://localhost:3000'. The other options do not match the intended port or indicate errors rather than successful startup.

  4. CRUD Operations

    Which HTTP method is typically used to create a new resource in a REST API?

    1. PATCH
    2. GET
    3. POST
    4. DELETE

    Explanation: POST is the standard HTTP verb for creating new resources. GET retrieves resources, DELETE removes them, and PATCH is commonly used for partial updates. Only POST is used for creation.

  5. API Routes and Data Handling

    What functionality does 'app.use(express.json())' provide in an Express-based REST API?

    1. It establishes a database connection
    2. It validates incoming data formats
    3. It handles URL routing for all endpoints
    4. It parses incoming request bodies as JSON

    Explanation: 'app.use(express.json())' middleware parses JSON payloads in incoming requests. It does not open database connections, perform URL routing, or validate data beyond parsing JSON structure.