Steps:
- Installation: To start with Fastify, you’ll need to install it via npm. Run:
npm install fastify
This installs the Fastify package and adds it to your node_modules
directory.
- Creating a Basic Server: Create a file named
index.js
and add the following code:
const Fastify = require('fastify'); const fastify = Fastify(); fastify.get('/', async (request, reply) => { return { hello: 'world' }; }); fastify.listen(3000, (err, address) => { if (err) { console.error(err); process.exit(1); } console.log(`Server listening at ${address}`); });
- This sets up a basic Fastify server that listens on port 3000 and responds with a JSON object
{ hello: 'world' }
when you access the root URL. - Running the Server: Start the server with:
node index.js
- Your server should now be running at
http://localhost:3000
.
Leave a Reply