Explore essential concepts for building a professional REST API using Node.js and Express.js, including setup, routing, and best practices for beginners.
Which Node.js framework is commonly used to simplify REST API development due to its built-in routing and middleware support?
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.
Which HTTP method should you use to create a new book record in a RESTful API?
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.
What feature of Node.js allows it to efficiently handle thousands of concurrent client requests?
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.
Which Express.js middleware is typically used to parse JSON data from incoming request bodies?
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.
In a simple Book API, which route is most appropriate for deleting a single book by its unique ID?
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.