Basic Fastify Setup

Steps:

  1. 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.
  1. 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}`); });
  1. 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.
  2. Running the Server: Start the server with:
node index.js 
  1. Your server should now be running at http://localhost:3000.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *