Node.js Day 8–10: Build Your First Backend API with Express.js Quiz

Explore essential concepts in creating your first backend API using Express.js, covering routing, HTTP methods, middleware, and best practices in modern Node.js backend development.

  1. Express.js Basics

    What is the main purpose of the Express.js framework in Node.js backend development?

    1. To compile Node.js code into machine language
    2. To simplify server and API creation by providing routing and middleware features
    3. To secure front-end web applications
    4. To replace JavaScript variables with strongly typed data

    Explanation: Express.js is designed to make building backend APIs and servers easier by streamlining routing, requests, and middleware usage. It does not focus on typing, code compilation, or front-end security, which are the focus of other tools or layers in web development.

  2. Project Initialization

    Which command quickly initializes a new Node.js project and skips interactive configuration steps?

    1. node start
    2. node create app
    3. npm install -q
    4. npm init -y

    Explanation: The command 'npm init -y' initializes a Node.js project and automatically accepts default settings, saving time during setup. The other options do not initialize projects or are invalid commands for this purpose.

  3. Routing Concepts

    In an Express.js app, what does the following route handler do? app.get('/hello', (req, res) => { res.send('Hello World'); });

    1. Accepts and logs user data from a form
    2. Renders an HTML page for all POST requests
    3. Sends 'Hello World' text when a GET request is made to '/hello'
    4. Returns a JSON array to any URL

    Explanation: The route uses app.get, so it only responds to GET requests on the '/hello' path by sending plain text. It does not render HTML, return JSON universally, or handle POST data.

  4. Handling JSON Requests

    Why is the line app.use(express.json()); important when building RESTful APIs with Express.js?

    1. It allows the server to automatically parse incoming JSON request bodies
    2. It secures all API endpoints by default
    3. It optimizes database queries for JavaScript objects
    4. It adds HTML rendering capabilities to the server

    Explanation: This middleware enables Express to parse JSON payloads for POST and PUT requests, exposing their contents as req.body. It does not impact security, HTML rendering, or database queries.

  5. HTTP Methods in REST APIs

    Which HTTP method should be used in Express.js to update existing data, and what does a typical route look like?

    1. PUT; app.put('/users/:id', (req, res) => { ... })
    2. GET; app.get('/update', (req, res) => { ... })
    3. DELETE; app.delete('/users/:id', (req, res) => { ... })
    4. POST; app.post('/users', (req, res) => { ... })

    Explanation: PUT requests are used to update existing resources like users with a specific id. GET retrieves data, DELETE removes resources, and POST creates new resources, so those are not used for updates.