Building a Complete REST API with Node.js, Express, TypeScript & MongoDB Quiz

Assess your understanding of designing production-ready REST APIs using Node.js, Express, TypeScript, and MongoDB, including setup, configuration, and best practices for backend development.

  1. Project Initialization and Dependencies

    Which command initializes a new Node.js project and adds TypeScript-specific development dependencies for building a REST API?

    1. npm init -y && npm install --save-dev typescript ts-node @types/express @types/node
    2. node init && npm install typescript --global
    3. npm start node && npm add typescript
    4. npm init app && npm install typescript ts-node

    Explanation: The command 'npm init -y' creates a Node.js project, and 'npm install --save-dev typescript ts-node @types/express @types/node' installs necessary development dependencies. The other options are incorrect because 'npm start node' is not valid, 'node init' does not initialize a project, and 'npm init app' is not the proper command for project setup.

  2. TypeScript Configuration Basics

    What is one key reason to specify 'outDir' and 'rootDir' in the TypeScript 'tsconfig.json' when structuring your backend project?

    1. To increase the speed of HTTP requests
    2. To set up environment variables for production
    3. To automatically install dependencies
    4. To separate source files from compiled JavaScript files

    Explanation: Specifying 'outDir' and 'rootDir' ensures that TypeScript keeps your source TypeScript files in one folder and outputs compiled JavaScript into another, improving organization and maintainability. Setting environment variables is unrelated, dependency installation does not relate to these options, and they do not affect HTTP request speed.

  3. Connecting to MongoDB in a Node.js REST API

    Which statement correctly describes how to securely connect your application to a MongoDB database?

    1. Use an HTTP API to send connection details on every request
    2. Hard-code the connection string into your source code for faster access
    3. Only connect to MongoDB after defining all models
    4. Store the MongoDB connection URI in an environment variable and load it using a configuration library like dotenv

    Explanation: Storing sensitive information like the database URI in environment variables enhances security and flexibility; loading them with a library such as dotenv is standard practice. Hard-coding credentials is insecure, models should be defined before or after establishing a connection as needed, and repeatedly sending credentials over an HTTP API is unsafe and inefficient.

  4. Middleware and Routing in Express

    What is the primary role of middleware functions like 'express.json()' in an Express-based REST API?

    1. To generate random user authentication tokens
    2. To render frontend templates server-side
    3. To define database schema mappings
    4. To parse incoming request bodies as JSON for use in routes and controllers

    Explanation: Middleware such as 'express.json()' automatically parses JSON data from HTTP request bodies, making it available in route handlers. Middleware does not set database schemas, does not handle server-side rendering of frontends, and does not generate authentication tokens.

  5. User Authentication with JWT

    Why is using JWT (JSON Web Token) recommended for securing REST API endpoints during user authentication?

    1. JWT eliminates the requirement for unique user models
    2. JWT automatically encrypts all database data
    3. JWT allows stateless authentication, enabling scalable and secure session management
    4. JWT replaces the need for middleware in Express

    Explanation: JWTs are used to securely transmit information between parties and support stateless authentication, which makes APIs scalable. JWT does not encrypt or store database data, does not replace middleware, and does not remove the need for defined user models.