Welcome to the Linux Foundation Forum!

has Creating a Web Server with Fastify (7) chapter an error?

jhonny111s
jhonny111s Posts: 22
edited September 2022 in LFW212 Class Forum

int his chapter we use "setNotFoundHandler" and it should return 405 and 404 but it does not return 404 but 200 (wrong status code) with a not found message.

that is my solution

fastify.setNotFoundHandler((request, reply) => {
    reply.status(404)  // ---> default Status error
    if (request.method !== 'GET') {
      reply.status(405)
      return 'Method Not Allowed\n'
    }
    return 'Not Found\n'
  })

Answers

  • lab 3.2 should validate 404 case

  • jhonny111s
    jhonny111s Posts: 22
    edited October 2022

    i found that we should use reply.callNotFound() to call the default behaviour.

      fastify.setNotFoundHandler((request, reply) => {
        if (request.method !== 'GET') {
          reply.status(405)
          return 'Method Not Allowed\n'
        }
        return reply.callNotFound()
      })
    
  • also we can return

          fastify.setNotFoundHandler((request, reply) => {
            if (request.method !== 'GET') {
              return reply.methodNotAllowed()
            }
            return reply.callNotFound()
          })
    
  • xdxmxc
    xdxmxc Posts: 110

    it's possible fastify behaviour has also changed slightly. this will be adjusted for in a coming update thanks @jhonny111s !

Categories

Upcoming Training