Building a simple REST API with NodeJS and Express. Quiz

This quiz covers the essentials of quickly setting up a REST API using NodeJS and Express, including project initialization, static file serving, and package management. Assess your foundational skills for efficient backend development with JavaScript.

  1. NodeJS and Its Purpose

    What is the primary use of NodeJS when building web applications?

    1. Running JavaScript on the server side
    2. Designing CSS for websites
    3. Creating only static HTML files
    4. Building mobile applications natively

    Explanation: NodeJS allows developers to execute JavaScript outside of the browser, enabling server-side programming. Creating static HTML files and designing CSS are front-end tasks, not specific to NodeJS. While NodeJS can be used alongside tools for mobile development, it does not natively build mobile apps.

  2. Initializing a New Project

    Which command interactively creates a package.json file when starting a new NodeJS project?

    1. express generate
    2. node start
    3. npm init
    4. npm install -g

    Explanation: The 'npm init' command guides users through creating a package.json file step-by-step. 'node start' runs a JavaScript file, 'npm install -g' installs global packages, and 'express generate' is not a valid command; the actual tool is 'express-generator'.

  3. Serving Static Files

    What does serving static files mean in the context of a NodeJS and Express backend?

    1. Encrypting files before sending to the browser
    2. Dynamically generating every file with JavaScript on each request
    3. Only processing data API requests
    4. Delivering unmodified files like HTML, CSS, and JS to the client

    Explanation: Serving static files refers to responding with pre-existing resources without modification. Dynamic file generation involves rendering on each request. Processing only data APIs excludes static content, and encrypting files is unrelated to standard static serving.

  4. Understanding Dependencies

    Why is it important to include dependencies in the package.json file?

    1. To back up static assets automatically
    2. To define server memory usage
    3. To list all packages required to run the app on any machine
    4. To store user account details securely

    Explanation: Listing dependencies ensures anyone installing the app can get the necessary packages automatically. The package.json file does not handle user accounts, server resource allocation, or backing up assets.

  5. Role of the -g Option

    In the command 'npm install -g express-generator', what is the role of the '-g' flag?

    1. Runs the package in the background
    2. Grants administrator access to npm
    3. Deletes existing packages
    4. Installs the package globally for use in any directory

    Explanation: The '-g' flag allows a package to be accessible from anywhere on the system. It does not make the package run in the background, delete packages, or provide admin access to npm.