Express Quiz

Challenge your understanding of Express with this quiz designed to assess your grasp of middleware, routing, request handling, and common usage scenarios in web development. Get insights into core Express concepts, ideal practices, and architectural patterns for building robust server-side applications.

  1. Basic Routing

    What HTTP verb is used when defining a route to handle form submissions in Express, such as '/submit-form'?

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

    Explanation: POST is commonly used for form submissions as it securely sends data to the server. GET is generally used for retrieving data and might expose sensitive form values in the URL. PUT is mostly used for updating existing resources, not for initial submissions. TRACE is seldom used and not suitable for submitting forms.

  2. Middleware Functionality

    In Express, what is the main purpose of middleware functions within the request-handling pipeline?

    1. To send emails
    2. To stop the server
    3. To process requests and pass control
    4. To generate static files

    Explanation: Middleware functions in Express process incoming requests, can modify request or response objects, and determine if the next handler should be invoked. They don't stop the server but rather help structure its behavior. Sending emails and generating static files are application-specific tasks, not inherent roles of middleware.

  3. Parameter Handling

    Which Express method allows you to extract URL parameters such as user IDs in routes like '/users/:id'?

    1. req.body
    2. req.params
    3. req.param
    4. req.query

    Explanation: req.params gives access to route parameters like ':id' in '/users/:id'. req.query is for query strings in the URL after '?', while req.body contains parsed payloads for POST or PUT. req.param is deprecated and less reliable for modern Express applications.

  4. Serving Static Files

    Which built-in middleware function in Express allows serving images, JavaScript, or CSS files from a public directory?

    1. express.route
    2. express.static
    3. express.middleware
    4. express.assets

    Explanation: express.static enables serving static assets such as images and scripts from a directory. express.middleware and express.route do not exist as middleware mechanisms. express.assets is not a built-in feature for this purpose.

  5. Error Handling Basics

    What characteristic distinguishes an error-handling middleware in Express from regular middleware?

    1. It returns HTML only
    2. It accepts four arguments
    3. It does not use parameters
    4. It only runs on POST requests

    Explanation: Error-handling middleware are identified by their four-argument signature: (err, req, res, next). Ordinary middleware typically has three arguments. Error-handlers are not limited to POST and can return any format, not just HTML.

  6. Listening to Requests

    How do you start an Express server to listen on port 5000?

    1. app.open(5000)
    2. app.run(5000)
    3. app.start(5000)
    4. app.listen(5000)

    Explanation: app.listen(5000) initiates the server to receive requests on port 5000. The other terms—app.start, app.run, and app.open—are not valid Express methods for starting a server.

  7. Route Matching

    Which route path would match both '/dog' and '/dog/', ensuring efficient handling of trailing slashes in Express?

    1. /dog?
    2. /dog/
    3. /dog*
    4. /dog

    Explanation: In Express, '/dog?' matches '/dog' with or without a trailing slash, thanks to the question mark. '/dog' matches only the exact path, '/dog/' expects a trailing slash, and '/dog*' matches any subpath starting with '/dog', which is broader than required.

  8. Order of Middleware

    Why is the order of middleware important in an Express application?

    1. It controls browser history
    2. It changes the HTTP method
    3. It sorts URL parameters
    4. It affects how requests are handled

    Explanation: Middleware is processed sequentially; earlier middleware can affect, transform, or halt subsequent processing. The other options, like changing HTTP methods or browser history, are inaccurate, and sorting URL parameters is unrelated to middleware order.

  9. JSON Data Processing

    Which middleware should be included to automatically parse incoming JSON payloads in Express routes?

    1. express.urlencoded()
    2. express.data()
    3. express.bodyParser()
    4. express.json()

    Explanation: express.json() parses incoming JSON requests automatically. express.urlencoded() is suited for form submissions and not pure JSON. express.data() is not an Express middleware. express.bodyParser() was common in earlier versions but is now obsolete.

  10. Dynamic Routing

    What is the correct Express route to handle all GET requests to any '/profile/:username' URL?

    1. app.get('/profile/:username')
    2. app.get('/profile/')
    3. app.get('/profile@username')
    4. app.get('/profile?username')

    Explanation: /profile/:username matches URLs containing a value after /profile/ and is how dynamic route parameters are defined in Express. '/profile/' will only match the base URL, '@username' is not syntax for parameterized routes, and '?username' would treat 'username' as a query, not part of the path.

  11. HTTP Status Codes

    Which method would you use to set the HTTP status code to 201 for a successful resource creation in an Express response?

    1. res.response(201)
    2. res.ok(201)
    3. res.setStatus(201)
    4. res.status(201)

    Explanation: res.status(201) sets the HTTP status code in Express responses, indicating successful creation. The other options are not valid Express methods; res.setStatus, res.response, and res.ok do not exist in Express.

  12. Chaining Middleware

    In Express, what is the correct way for a middleware function to delegate control to the next function in the stack?

    1. pass()
    2. next()
    3. goNext()
    4. return

    Explanation: Calling next() passes processing to the next middleware or route handler. Simply returning would exit the function but not advance. pass() and goNext() are not valid Express function calls.

  13. Query Strings

    To access the value of '?search=dog' in the Express URL '/pets?search=dog', which property should be used?

    1. req.data.search
    2. req.params.search
    3. req.query.search
    4. req.body.search

    Explanation: req.query.search retrieves the value from the query string in Express. req.params is for route parameters, req.body is for POSTed data, and req.data is not an Express property.

  14. Sending Responses

    Which response method should be used to send a JSON object back to a client in Express?

    1. res.send()
    2. res.render()
    3. res.html()
    4. res.json()

    Explanation: res.json() serializes and sends an object as JSON. res.send() can send many types but doesn't always set headers appropriately for JSON. res.render() is for templates, and res.html() is not a valid Express response method.

  15. Default 404 Handling

    How should you add a default 404 response for undefined routes at the end of an Express router?

    1. By using next('404') in middleware
    2. By defining a route at the beginning
    3. By placing a catch-all '*' route last
    4. By defining res.status(404) after all routes

    Explanation: A route with '*' as the path, placed last, will catch all unmatched requests. Defining at the beginning would overshadow all others, using next('404') does not default to custom 404 unless handled, and directly setting res.status(404) outside a handler has no effect.

  16. Application Locals

    When storing data accessible in every rendered view, which Express property should you use?

    1. app.locals
    2. app.cache
    3. app.cookies
    4. app.res

    Explanation: app.locals provides a shared storage area for application-wide data, accessible in all views. app.cookies is not a valid property, app.res refers to response objects not global data, and app.cache is unrelated to locals.