Welcome to the Linux Foundation Forum!

05. CREATING RESTFUL JSON SERVICES - NotFound() function problem

Link: https://trainingportal.linuxfoundation.org/learn/course/nodejs-services-development-lfw212/creating-restful-json-services/creating-restful-json-services?page=3

Title: Implementing a RESTful JSON GET with Fastify (Cont.)

Note:

Line: if (err.message === 'not found') reply.notFound()

when I test a not found page fastify show this error:
TypeError: reply.notFound is not a function
[fastify-cli] app crashed - waiting for file changes before starting…

So I check de Fastify documentation and I found that using callNotFound() it would work. So I change the code to

if (err.message === 'not found') reply.callNotFound()

  • Be careful with the next exercise, it has a similar behavior

Comments

  • ariel15584
    ariel15584 Posts: 4
    edited October 2020

    Have you tried registering the fastify-sensible plugin?

    fastify.register(require('fastify-sensible'))

    It worked for me with that, but the instruction to register the plugin is missing in the course (maybe that's premeditated?)

  • davidmarkclements
    davidmarkclements Posts: 270
    edited November 2020

    hey @ariel15584 and @canelacho thanks both for the feedback on this - @ariel15584 is correct that it needs to be registered by this is accidental omission not premeditation. The following that section has been updated to specify registering fastify-sensible

    As for reply.callNotFound, fastify-sensible is more recommended for the following reasons:

    • reply.callNotFound this can be used instead but does not cater quite as well to async/await handlers (e.g. you can't throw it)
    • reply.callNotFound does not generate a call stack, it only creates a 404 response. This is very good for performance, but less good for debugging and maintenance.
    • fastify-sensible supplies other types of HTTP errors as well, and is generally used across the industry at this point

Categories

Upcoming Training