पाठ 7 / 32

बेसिक रूटिंग

मानक CRUD operations से जुड़े GET, POST, PUT, PATCH और DELETE routes define करें।

HTTP methods

Fastify fastify.get(), fastify.post(), fastify.put(), fastify.delete(), fastify.patch(), और fastify.head() को expose करता है हर HTTP verb के लिए।

RESTful resource routes

विशिष्ट CRUD patterns मानक HTTP verbs से map होते हैं।

// List all
fastify.get('/items', async () => []);

// Create one
fastify.post('/items', async (req, reply) => {
  return { id: 1, created: true };
});

// Read one
fastify.get('/items/:id', async (req) => {
  return { id: req.params.id };
});

// Update one
fastify.put('/items/:id', async (req) => {
  return { id: req.params.id, updated: true };
});

// Delete one
fastify.delete('/items/:id', async (req, reply) => {
  return reply.status(204).send();
});

त्वरित जाँच

त्वरित जाँच: किसी resource को आंशिक रूप से अपडेट करने के लिए कौन-सी HTTP method का उपयोग किया जाता है?

  • PUT
  • POST
  • DELETE
  • PATCH
Answer

PATCH — PATCH आंशिक updates के लिए है; PUT पूरे resource को replace करता है।

shorthand बनाम route()

fastify.get(path, handler) shorthand है। जब एक route को कई methods या बहुत options चाहिए, तो fastify.route({ method, url, schema, handler }) use करें।