API Middlewares

What is Middleware?

Middleware means middleware functions. These functions have access to multiple things:

  • The request object (req)

  • The response object (res)

  • And the next function in the application's cycle.

What does middleware have to do with API's and Next?

API routes in Next.js provide some built in middlewares that can parse incoming requests (req).

These middlewares are:

  • req.cookies: this is an object that contains cookies that are sent by the request.

    • Default is {}.

  • req.query: this is an object that contains the query string.

    • Default is {}.

  • req.body: this is an object that contains the body that is parsed by content-type but can be null if there was never a body sent.