← Back to Node.js Course | Chapter 7: Express.js Basics | Lesson 3 of 7

Routes क्या हैं

Routes एक method और एक URL path को उस function से map करते हैं जो इसे handle करता है।

In this page:

  1. Routes
Syntax
javascript
app.get('/path', (req, res) => {
  res.send('response');
});
app.post('/path', handler);
app.put('/path/:id', handler);
app.delete('/path/:id', handler);

Routes

app.get, app.post, app.put, और app.delete से routes define करें। Paths में /users/:id जैसे parameters हो सकते हैं जो req.params में available हैं। Routes define होने के order में check होती हैं और respond करने वाली पहली जीतती है।

Note: अलग files में related routes group करने के लिए express.Router इस्तेमाल करें।

उदाहरण: Routes

bash
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("home"));
app.get("/users/:id", (req, res) => res.json({ id: req.params.id }));
app.post("/users", express.json(), (req, res) => res.status(201).json(req.body));
app.listen(3000, () => console.log("listening on 3000"));
// GET /users/42  ->  {"id":"42"}

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. specific routes से पहले एक catch-all define करना
  2. respond करना भूल जाना
  3. गलत HTTP method इस्तेमाल करना
चैप्टर सारांश
  • app.METHOD(path, handler) pattern इस्तेमाल होता है
  • :id params बनाता है
  • Order matter करता है
  • Router routes group करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.