Welcome to the Linux Foundation Forum!

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

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

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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Answers

  • lab 3.2 should validate 404 case

  • Posts: 22
    edited October 2022

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

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

    1. fastify.setNotFoundHandler((request, reply) => {
    2. if (request.method !== 'GET') {
    3. return reply.methodNotAllowed()
    4. }
    5. return reply.callNotFound()
    6. })
  • Posts: 160

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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training