Fastify interview questions

22 questions from the Fastify: Building High-Performance Node.js APIs course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. Why does Fastify use async/await?

  1. It's required by Node.js
  2. Callbacks are deprecated
  3. To handle I/O operations cleanly and maintain performance
  4. It makes the server single-use
Show answer

Answer: To handle I/O operations cleanly and maintain performance

Fastify is built on async/await to handle asynchronous operations efficiently and keep the non-blocking event loop clean.

Lesson: Your First Server

Module 2: Foundation

2. Which sets a status code and sends JSON in one chain?

  1. `reply.code(201).send({id: 1})`
  2. `request.code(201)`
  3. `reply.json(201)`
Show answer

Answer: `reply.code(201).send({id: 1})`

`reply.code()` returns the reply, so `.send()` chains.

Lesson: Request & Reply Objects

3. What must a plugin function receive as its first argument?

  1. The Node http server
  2. A Fastify instance (encapsulated) and options
  3. Only the options object
Show answer

Answer: A Fastify instance (encapsulated) and options

The signature is `(instance, opts)` plus `done` unless it is async.

Lesson: Plugins & Decorators

Module 3: Core Routing & Handlers

4. Which HTTP method is used to partially update a resource?

  1. PUT
  2. POST
  3. DELETE
  4. PATCH
Show answer

Answer: PATCH

PATCH is for partial updates; PUT replaces the entire resource.

Lesson: Basic Routing

Module 4: Data Handling & Validation

5. Why does a response schema make Fastify faster?

  1. It caches the database
  2. It compiles a specialised serializer instead of generic JSON.stringify
  3. It disables logging
Show answer

Answer: It compiles a specialised serializer instead of generic JSON.stringify

fast-json-stringify generates serialization code from the schema, and it also drops undeclared fields.

Lesson: JSON Schema & Validation

Module 5: Design Patterns & Hooks

6. Which hook runs after a request is validated but before the handler?

  1. preHandler
  2. onRequest
  3. preValidation
  4. onSend
Show answer

Answer: preHandler

preHandler runs after validation and before the route handler. Use it for auth checks or last-minute data preparation.

Lesson: Lifecycle Hooks

7. Which hook runs before the body is parsed?

  1. preHandler
  2. onRequest
  3. onSend
Show answer

Answer: onRequest

Order: onRequest, preParsing, preValidation, preHandler. The body is available from preValidation onward.

Lesson: Lifecycle Hooks

8. How do you make a plugin's decorator visible to the parent instance?

  1. Name the plugin `global`
  2. Wrap it with `fastify-plugin`
  3. Call `decorate` twice
Show answer

Answer: Wrap it with `fastify-plugin`

`fastify-plugin` skips encapsulation so decorators and hooks apply to the parent.

Lesson: Encapsulation & Scope

Module 6: Practical Implementation

9. Why prefer `app.inject()` over starting a server and calling fetch in unit tests?

  1. It is faster and needs no free port
  2. It skips all hooks
  3. It works only for POST
Show answer

Answer: It is faster and needs no free port

inject runs the full lifecycle in-process, so hooks and validation still apply.

Lesson: Testing Routes

Module 7: Quality & Reliability

10. What status does Fastify use for an uncaught `throw new Error('x')` in a handler by default?

  1. 400
  2. 500
  3. 200
Show answer

Answer: 500

Errors without a statusCode become 500 Internal Server Error.

Lesson: Error Handling

11. How do you turn on the built-in Pino logger?

  1. `Fastify({ logger: true })`
  2. `npm i pino` only
  3. It is always on
Show answer

Answer: `Fastify({ logger: true })`

Logging is off by default; `logger: true` enables Pino with per-request ids.

Lesson: Logging

Module 10: Best Practices & Interview

12. Why is Fastify faster than Express?

  1. It uses a different programming language
  2. It has lower overhead through object pooling, efficient routing, and built-in validation
  3. It doesn't support middleware
  4. It runs only in the browser
Show answer

Answer: It has lower overhead through object pooling, efficient routing, and built-in validation

Fastify reuses request/reply objects, optimizes routing with a trie-based router, and validates input with JSON Schema—all designed for performance.

Lesson: Interview Questions & Tips

13. What is the purpose of Fastify plugins?

  1. To add CSS styling to routes
  2. To replace Express middleware
  3. To encapsulate and reuse functionality (auth, DB, logging) across routes
  4. To generate HTML templates only
Show answer

Answer: To encapsulate and reuse functionality (auth, DB, logging) across routes

Plugins enable modular, maintainable code by separating concerns and allowing reusable components to be registered across the app.

Lesson: Interview Questions & Tips

14. How does Fastify handle async/await in route handlers?

  1. It wraps them in try/catch automatically
  2. Async handlers are not supported
  3. It requires manual promise chains
  4. It catches rejected promises and passes errors to the error handler
Show answer

Answer: It catches rejected promises and passes errors to the error handler

Fastify is built on async/await and automatically catches rejected promises, routing them to the setErrorHandler.

Lesson: Interview Questions & Tips

15. Which Fastify method sends a request without a real server?

  1. fastify.listen()
  2. fastify.close()
  3. fastify.inject()
  4. fastify.log()
Show answer

Answer: fastify.inject()

inject simulates HTTP requests for fast tests.

Lesson: Interview Questions & Tips

16. What is Fastify's built-in logger?

  1. Winston
  2. Bunyan
  3. Morgan
  4. Pino
Show answer

Answer: Pino

Pino is fast structured JSON logging.

Lesson: Interview Questions & Tips

17. What does a response schema improve?

  1. Serialization speed and data safety
  2. CSS size
  3. Git history
  4. DNS lookups
Show answer

Answer: Serialization speed and data safety

Only listed fields are serialized.

Lesson: Interview Questions & Tips

18. Which lifecycle order is correct?

  1. preHandler, onRequest, onSend
  2. onRequest, preValidation, preHandler, onSend
  3. onSend, onRequest
  4. preValidation, onRequest
Show answer

Answer: onRequest, preValidation, preHandler, onSend

Hooks run in a defined sequence for each request.

Lesson: Interview Questions & Tips

19. What does plugin encapsulation provide?

  1. Global variables
  2. Faster CPU
  3. Scoped decorators, hooks and routes
  4. Automatic HTTPS
Show answer

Answer: Scoped decorators, hooks and routes

Child plugin changes do not leak to siblings.

Lesson: Interview Questions & Tips

20. Which plugin handles multipart file uploads?

  1. @fastify/cors
  2. @fastify/jwt
  3. @fastify/helmet
  4. @fastify/multipart
Show answer

Answer: @fastify/multipart

It parses multipart/form-data.

Lesson: Interview Questions & Tips

21. Why keep a Fastify app stateless when scaling?

  1. Any instance can serve any request
  2. It uses less RAM
  3. It disables logging
  4. It avoids HTTPS
Show answer

Answer: Any instance can serve any request

Shared state belongs in Redis or a database.

Lesson: Interview Questions & Tips

22. Which header-setting plugin adds protective HTTP headers?

  1. @fastify/static
  2. @fastify/helmet
  3. @fastify/multipart
  4. @fastify/jwt
Show answer

Answer: @fastify/helmet

Helmet sets common security headers.

Lesson: Interview Questions & Tips

← Back to the course