Node.js API Tutorial: Build Your First REST API Quiz

Explore essential concepts for building a professional REST API using Node.js and Express.js, including setup, routing, and best practices for beginners.

  1. Selecting a Framework

    Which Node.js framework is commonly used to simplify REST API development due to its built-in routing and middleware support?

    1. Express.js
    2. React.js
    3. MongoDB
    4. Moment.js

    Explanation: Express.js is designed for building web servers and APIs, offering straightforward routing and a vast middleware ecosystem. React.js is a frontend library, not for APIs. Moment.js deals with dates and times, not server logic. MongoDB is a database, not a server framework.

  2. API Endpoint Methods

    Which HTTP method should you use to create a new book record in a RESTful API?

    1. GET
    2. PUT
    3. POST
    4. DELETE

    Explanation: POST is used to create new resources in REST APIs. GET retrieves data, PUT updates existing resources, and DELETE removes resources. Using POST correctly follows REST conventions.

  3. Handling Concurrent Requests

    What feature of Node.js allows it to efficiently handle thousands of concurrent client requests?

    1. Blocking filesystem calls
    2. Synchronous loops
    3. Multithreaded core
    4. Non-blocking I/O

    Explanation: Non-blocking I/O enables Node.js to process many requests without waiting, making it performant for APIs. Multithreaded core is not used by default; Node.js uses a single-threaded event loop. Synchronous loops and blocking filesystem calls can hinder performance.

  4. Parsing Incoming Data

    Which Express.js middleware is typically used to parse JSON data from incoming request bodies?

    1. express.static()
    2. express.Router()
    3. express.json()
    4. express.session()

    Explanation: express.json() parses incoming JSON payloads, making data available in req.body. express.static() serves static files. express.Router() creates modular route handlers. express.session() handles session management, not JSON parsing.

  5. Building CRUD Endpoints

    In a simple Book API, which route is most appropriate for deleting a single book by its unique ID?

    1. /books/:id (DELETE)
    2. /books/create (PUT)
    3. /books/all (GET)
    4. /books/:id (POST)

    Explanation: The route /books/:id with the DELETE method targets the specific book using its ID. /books/all with GET retrieves all books, not deletion. /books/:id with POST is for creating data, and /books/create with PUT is not a standard REST pattern for deletion.