Theory
Methods और URLs साथ
FestConnect के backend को front end को event list पढ़ने, एक registration add करने, एक detail update करने, और एक entry delete करने देना पड़ता है। इनमें से हर एक एक route है: एक HTTP method (कौन सा action) की एक URL path (कौन सा resource) के साथ pairing।
चार core methods चार basic data actions से neatly map होते हैं। यह lesson दिखाता है कैसे Express GET, POST, PUT, और DELETE के लिए routes define करता है, कैसे एक handler request से parameters पढ़ता है और एक response भेजता है, और logic को controllers में tidy कैसे रखें। यही एक REST API build करने का heart है।
At a glance
| Method | Action | FestConnect Example |
|---|---|---|
| GET | Data पढ़ना | GET /api/events, events list कीजिए |
| POST | नया data create करना | POST /api/registrations, एक registration add कीजिए |
| PUT | Existing data update करना | PUT /api/events/5, event 5 edit कीजिए |
| DELETE | Data remove करना | DELETE /api/events/5, event 5 delete कीजिए |
Practical
चार Methods के लिए Routes Define करना
app.get('/api/events', (req, res) => {
res.json(events); // read: send the list
});
app.post('/api/events', (req, res) => {
const newEvent = req.body; // create: data is in the request body
events.push(newEvent);
res.status(201).json(newEvent); // 201 = Created
});
app.put('/api/events/:id', (req, res) => { /* update event :id */ });
app.delete('/api/events/:id', (req, res) => { /* remove event :id */ });This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Route Parameters और Query Parameters
Requests data को दो common URL forms में carry करते हैं।
Route parameters path के named parts हैं, एक colon के साथ लिखे गए: /api/events/:id में, :id एक placeholder है। /api/events/5 को एक request के लिए, Express आपको req.params.id देता है '5' के बराबर। एक specific resource identify करने के लिए इन्हें इस्तेमाल कीजिए।
Query parameters URL में एक ? के बाद key-value pairs की तरह आते हैं: /api/events?category=tech। Express इन्हें req.query से पढ़ता है, तो req.query.category 'tech' है। Filters, searches, और options के लिए इन्हें इस्तेमाल कीजिए। तो route params बताते हैं कौन सा resource; query params refine करते हैं कैसे आप इसे चाहते हैं।
Practical
Route और Query Parameters पढ़ना
// Route parameter: which event? (/api/events/5)
app.get('/api/events/:id', (req, res) => {
const id = req.params.id; // '5'
res.json(findEvent(id));
});
// Query parameter: filter the list (/api/events?category=tech)
app.get('/api/events', (req, res) => {
const category = req.query.category; // 'tech' (or undefined)
res.json(category ? filterByCategory(category) : events);
});This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
Front end को इसकी id से एक specific event DELETE करने देने के लिए, कौन सा HTTP method और path shape REST conventions fit करता है?
- GET /api/events, क्योंकि GET सब कुछ करता है
- DELETE /api/events/:id, id के लिए एक route parameter के साथ DELETE method इस्तेमाल करते हुए
- POST /api/deleteEvent, क्योंकि POST actions के लिए है
- PUT /api/events, क्योंकि PUT data remove करता है
Show the answer
DELETE /api/events/:id, id के लिए एक route parameter के साथ DELETE method इस्तेमाल करते हुए
REST DELETE method को एक resource remove करने में map करता है, और एक route parameter identify करता है कौन सा, तो DELETE /api/events/:id (जैसे /api/events/5) conventional shape है। Option A GET misuse करता है, जो data READ करने के लिए है, इसे delete करने के लिए नहीं (और data change करने के लिए GET इस्तेमाल करना REST तोड़ता है और unsafe है)। Option C technically काम करता है पर REST conventions ignore करता है: action HTTP method (DELETE) में belong करता है, POST के साथ URL में /deleteEvent की तरह baked नहीं होता। Option D wrong है: PUT एक existing resource UPDATE करने के लिए है, इसे remove करने के लिए नहीं। Method को action से match कीजिए: GET read, POST create, PUT update, DELETE remove, एक specific item target करने के लिए एक route parameter के साथ।
Think first
URL की बजाय HTTP Method को Action Carry क्यों करने दें?
आप URLs को /getEvents और /deleteEvent जैसा नाम दे सकते थे। REST action को method में क्यों डालता है और URLs को resources के बारे में क्यों रखता है? फिर tap कीजिए।
Show the answer
क्योंकि RESOURCE (URL) को ACTION (HTTP method) से separate करना एक clean, predictable, और standard API देता है, जबकि URLs में verbs bake करना एक inconsistent sprawl की तरफ ले जाता है। REST में, एक URL एक THING को नाम देता है, /api/events 'events' है, /api/events/5 'event 5' है, और METHOD बताता है आप उस thing के साथ क्या करना चाहते हैं: इसे GET करना, एक नई POST करना, इसे PUT (update) करना, इसे DELETE करना। इसका मतलब है एक URL अलग methods के through कई operations support करता है, और कोई भी developer API guess कर सकता है: event 5 update करने के लिए वे बिना custom verb-URLs की एक list consult किए जानते हैं यह PUT /api/events/5 है। Alternative, /getEvents, /addEvent, /deleteEvent, /updateEvent, जल्दी inconsistent names की एक jungle बन जाता है (क्या यह /removeEvent है या /deleteEvent? /events/new या /addEvent?), और यह HTTP methods waste करता है जो पहले से exactly इन actions express करने के लिए exist करते हैं। Methods इस्तेमाल करना web के infrastructure को भी आपकी help करने देता है: GET को safe और cacheable समझा जाता है, DELETE और PUT को data change करने वाला समझा जाता है, तो browsers, proxies, और tools sensibly behave करते हैं। यह security reasoning भी improve करता है (आप कभी एक GET से data change नहीं करते)। तो REST की discipline, URL में nouns, method में verbs, pedantry नहीं है; यह ऐसे APIs produce करती है जो consistent, discoverable हैं, और web के साथ अच्छी तरह काम करते हैं। Path में resources, method में actions।
Summary
Key takeaways
- एक route एक HTTP method को एक URL path और एक handler function के साथ pair करता है।
- चार core methods CRUD में map होते हैं: GET reads करता है, POST creates करता है, PUT updates करता है, DELETE removes करता है।
- Express इन्हें app.get/post/put/delete(path, handler) से define करता है; handler को (req, res) मिलता है।
- res.json() या res.send() से responses भेजिए, और res.status() से status set कीजिए (जैसे 201 Created)।
- Route parameters path segments name करते हैं (/events/:id, req.params.id से पढ़ा गया) एक resource identify करने के लिए।
- Query parameters URL में ? follow करते हैं (/events?category=tech, req.query.category से पढ़ा गया) filters और options के लिए।
- Memory hook: URL में resource, method में action; path में :id, query में filters।