Quiz about Middleware Quiz

Middleware Quiz

Last Updated :
Discuss
Comments

Question 1

In the context of the 'express.json()' middleware, what is the purpose of the 'reviver' option?

  • A

    To set a limit on the size of the JSON payload.

  • B

    To transform the parsed JSON object before it is made available in 'req.body'.

  • C

    To revive a server that has crashed after receiving a JSON payload.

  • D

    To specify the content-type that the middleware will parse.

Question 2

When using 'express.urlencoded({ extended: false })', how would the query string 'user[name]=John&user[email]=john@example.com' be parsed in 'req.body'?

  • A

    { user: [ 'John', 'john@example.com' ] }

  • B

    { user: { name: 'John', email: 'john@example.com' } }

  • C

    It would throw an error because nested objects are not supported.

  • D

    { 'user[name]': 'John', 'user[email]': 'john@example.com' }

Question 3

Which of the following statements most accurately describes the primary role of middleware in a distributed system?

  • A

    It provides a simplified, high-level interface for application developers to interact with the operating system's core functionalities.

  • B

    It directly manages hardware resources for the operating system.

  • C

    It acts as a 'software glue' that enables communication and data management for disparate applications, often across a network.

  • D

    It is a type of application software that provides services directly to the end-user.

Question 4

In Morgan.js, what is the primary difference in logging behavior when the 'immediate' option is set to 'true'?

  • A

    It only logs requests that result in an immediate server error.

  • B

    It provides a more detailed log format.

  • C

    It logs the request as soon as the headers are received, before the response is sent.

  • D

    It logs the request immediately to a file instead of the console.

Question 5

What happens if you send a JSON payload with a 'Content-Type' of 'application/json' to an Express app that only uses 'express.urlencoded({ extended: true })' and not 'express.json()'?

  • A

    The application will crash with a 'TypeError'.

  • B

    The server will automatically parse the JSON into 'req.body'.

  • C

    'req.body' will contain the raw JSON string.

  • D

    'req.body' will be an empty object '{}'.

Question 6

How would you configure Morgan.js to log all requests with a status code of 400 or higher, and ignore all others?

  • A

    app.use(morgan('combined', { stream: (req, res) => res.statusCode >= 400 }))

  • B

    app.use(morgan('combined', { skip: (req, res) => res.statusCode < 400 }))

  • C

    app.use(morgan('combined', { immediate: true, status: '>= 400' }))

  • D

    app.use(morgan.skip((req, res) => res.statusCode < 400))

Question 7

Which statement correctly identifies the relationship between 'express.json()' and the 'body-parser' library?

  • A

    'express.json()' and 'body-parser.json()' are competing libraries that should not be used together.

  • B

    'body-parser' is now deprecated and 'express.json()' is the official replacement.

  • C

    'express.json()' is essentially 'body-parser.json()' integrated directly into the Express framework.

  • D

    'express.json()' is a lightweight alternative to 'body-parser' with fewer features.

Question 8

The 'express.urlencoded()' middleware uses the 'qs' library when 'extended: true'. What is a key advantage of 'qs' over the default 'querystring' library?

  • A

    It can parse nested objects and arrays from the URL-encoded data.

  • B

    It provides better performance for simple, flat query strings.

  • C

    It allows for larger payloads to be processed.

  • D

    It automatically sanitizes all input to prevent XSS attacks.

Question 9

What is the purpose of the 'inflate' option in 'express.json()' and 'express.urlencoded()'?

  • A

    To increase the payload size limit by 'inflating' it.

  • B

    To expand shorthand JSON into a more verbose format.

  • C

    To convert a flat object into a nested object.

  • D

    To automatically decompress request bodies with 'Content-Encoding' of 'gzip' or 'deflate'.

Question 10

Consider the Morgan.js custom format: 'morgan(':method :url :status :res[content-length] - :response-time ms')'. Which part of this is a custom token definition?

  • A

    ':method'

  • B

    ':response-time'

  • C

    None of the above

  • D

    ':res[content-length]'

There are 10 questions to complete.

Take a part in the ongoing discussion