Express.js, a minimalist web framework for Node.js, has become an essential tool for many web developers. This quiz is designed to test your foundational knowledge of Express.js. For each question, choose the best answer. Explanations are provided for each correct answer to help you understand the concepts better.
1. What is Express.js?
Answer:
Explanation:
Express.js is a minimalist web server framework for Node.js that simplifies the process of building web applications.
2. Which method sends a JSON response in Express.js?
Answer:
Explanation:
The res.json() method sends a JSON response.
3. How do you capture query parameters in Express routes?
Answer:
Explanation:
Query parameters are captured using the req.query object.
4. Which of these is not a core feature of Express?
Answer:
Explanation:
While Express provides middleware, routing, and templating, it does not have an in-built Object-Relational Mapping (ORM) system.
5. To serve static files like images, CSS files, and JavaScript files in Express, you use:
Answer:
Explanation:
The express.static() middleware function is used to serve static files.
6. In Express.js, how do you redirect a user to another page?
Answer:
Explanation:
The res.redirect() method is used to redirect the user to another URL.
7. What’s the primary use of middleware in Express?
Answer:
Explanation:
Middleware functions have access to the request and response objects and are used to execute any code, end the request-response cycle, or invoke the next middleware in the stack.
8. What method is used to create a middleware in Express?
Answer:
Explanation:
The app.use() method is used to mount a middleware function.
9. How do you send a status of 404 and a message ‘Not Found’ in Express?
Answer:
Explanation:
The res.status().send() combination allows you to set a specific status code and send a message.
10. Which of these methods will handle HTTP POST requests in an Express app?
Answer:
Explanation:
The app.post() method is specifically designed to handle POST requests.
11. How do you capture data sent in the body of a POST request in Express?
Answer:
Explanation:
req.body contains data sent in the body of the request, typically used for POST data.
12. To use req.body, what middleware must be used with Express?
Answer:
Explanation:
express.urlencoded() is a middleware that parses incoming requests with URL-encoded payloads, thus populating req.body.
13. Which middleware in Express.js helps in handling JSON data from POST requests?
Answer:
Explanation:
express.json() is a built-in middleware in Express.js that parses incoming requests with JSON payloads.
14. Which of the following methods is used to render a view in Express.js?
Answer:
Explanation:
res.render() is used to render a view template.
15. If you want to set a cookie in Express.js, which method would you use?
Answer:
Explanation:
The res.cookie() method is used to set cookies.
16. In Express, if you wanted a middleware function to be executed for every request, where would you place it?
Answer:
Explanation:
Placing the middleware before all route definitions ensures it gets executed for every request before specific route logic is handled.
17. What will next() function do in Express middleware?
Answer:
Explanation:
In Express, the next() function, when called within middleware, passes control to the next middleware in line.
18. To serve static files in Express, which built-in middleware would you use?
Answer:
Explanation:
The express.static() middleware is used to serve static files in an Express application.
19. What does app.all() method do in Express.js?
Answer:
Explanation:
The app.all() method is a routing method to specify a callback function that gets executed when an endpoint (a combination of a request method and route) is hit.
20. Which function in Express.js wraps an asynchronous function to catch all the errors?
Answer:
Explanation:
Though this isn’t a built-in Express function, wrapAsync() is commonly used in the community to handle errors in asynchronous functions without needing to always use try-catch.
21. If you want to set several properties on a response at once in Express.js, which method would you use?
Answer:
Explanation:
The res.set() method is used in Express.js to set multiple headers in the response object.
22. How can you access the query string parameters in Express.js?
Answer:
Explanation:
The req.query object contains the query string parameters in Express.js.
23. What is the primary purpose of the Express ‘Router’?
Answer:
Explanation:
Express ‘Router’ is used to create modular, mountable route handlers in separate file(s) or modules.
24. How do you get the HTTP method (like GET, POST) of a client’s request in Express?
Answer:
Explanation:
The req.method property contains a string representing the HTTP method of the request: GET, POST, PUT, and so on.
25. How do you retrieve the value of a specific cookie sent in a request in Express.js?
Answer:
Explanation:
To retrieve the value of a specific cookie, you use req.cookies[cookieName] where cookieName is the name of the cookie you want to access.