🚀 Building a Scalable REST API with Node.js, Express, and TypeScript Quiz

Enhance your backend development knowledge by exploring clean architecture, best practices, and essential tools for building scalable REST APIs using Node.js, Express, and TypeScript.

  1. Project Initialization and Essential Packages

    Which set of commands and packages is most appropriate to set up a new Node.js REST API project with TypeScript and Express, including type definitions and a convenient dev environment?

    1. npm init, npm install express, npm run devserver
    2. npm install typescript express, npm start
    3. npm init -y, npm install express dotenv, npm install -D typescript ts-node-dev @types/node @types/express
    4. npm init -y, npm install mongoose, npm install -D ts-node nodemon

    Explanation: The correct option includes initializing the project and installing Express, dotenv, TypeScript, ts-node-dev for live reloading, and type definitions for Node and Express, which are essential for a TypeScript backend. Other options are either missing key dependencies, type definitions, or do not provide a suitable development workflow.

  2. Organizing Project Structure

    Given the need for scalability and clean code, which folder structure best organizes an Express TypeScript API project for features, configuration, and error handling?

    1. app, bin, modules, logs, data
    2. server, assets, common, dist
    3. src/config, controllers, routes, services, middlewares, utils, app.ts, server.ts, types
    4. src/api, core, tests, scripts, views

    Explanation: This structure separates configuration, business logic, route handlers, error handling, utility functions, and type definitions, promoting modularity and scalability. The other options mix unrelated folders or do not clearly map to backend API best practices.

  3. Error Handling Middleware

    How is centralized error handling implemented in an Express API using TypeScript to ensure all errors are properly formatted?

    1. By placing all validation inside controller functions
    2. By using try/catch blocks inside every request handler without middleware
    3. By creating an error-handling middleware that catches errors and sends a consistent JSON response
    4. By logging errors only to the console

    Explanation: Centralized error-handling middleware captures errors occurring in the request lifecycle and sends formatted JSON responses. Relying only on try/catch within handlers or logging to console does not guarantee consistent error handling. Validation inside controllers is useful but does not replace structured error middleware.

  4. Type Definitions and Custom Request Properties

    What is the recommended way to extend the Express Request object with a custom property when using TypeScript?

    1. Edit the Express source code
    2. Add the property dynamically at runtime without any typings
    3. Declare a new property in a type definition file under the Express namespace
    4. Declare the property in each controller file

    Explanation: The correct approach is to extend types via declaration merging in a d.ts file, allowing TypeScript to recognize custom fields. Dynamically adding properties or editing source files is unsafe and against best practices. Declaring in every controller leads to redundancy.

  5. Input Validation Implementation

    Which method enables input validation in an Express TypeScript API to catch and respond to invalid client data before reaching controllers?

    1. Creating middleware that parses and checks the request body using a schema validation library
    2. Validating all input in the database layer
    3. Using only TypeScript interfaces for type checking at runtime
    4. Relying solely on frontend validation

    Explanation: Middleware using schema validators like Zod or Joi efficiently stops invalid data before controllers process it. Database-layer validation delays error feedback, TypeScript interfaces offer only compile-time safety, and frontend validation alone is insufficient and untrustworthy.