Lesson 3 / 30
Your First Server
A running Fastify server in under ten lines.
Hello, World!
Create an instance, define one route, and start the server. Notice the async nature—Fastify is async-first.
import Fastify from 'fastify';
const fastify = Fastify();
fastify.get('/', async (request, reply) => {
return { message: 'Hello, World!' };
});
await fastify.listen({ port: 3000 });
console.log('Server running on http://localhost:3000');
Output:
Server running on http://localhost:3000
The Fastify instance
Fastify() creates the server instance. fastify.get(route, handler) registers a GET route. await fastify.listen() starts the HTTP server with graceful shutdown support.
Quick check: Why does Fastify use async/await?
- It's required by Node.js
- To handle I/O operations cleanly and maintain performance
- Callbacks are deprecated
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.