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.
Which command initializes a new Node.js project and adds TypeScript-specific development dependencies for building a REST API?
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.
What is one key reason to specify 'outDir' and 'rootDir' in the TypeScript 'tsconfig.json' when structuring your backend project?
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.
Which statement correctly describes how to securely connect your application to a MongoDB database?
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.
What is the primary role of middleware functions like 'express.json()' in an Express-based REST API?
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.
Why is using JWT (JSON Web Token) recommended for securing REST API endpoints during user authentication?
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.