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

Request और Response

Express request और response objects में friendly helpers add करता है।

In this page:

  1. Request/Response
Syntax
javascript
req.params.name
req.query.name
req.body
res.json(data);

Request/Response

request पर आपके पास req.params, req.query, req.body (एक parser के बाद), और headers के लिए req.get है। response पर res.send, res.json, res.status, res.redirect, और res.sendFile इस्तेमाल करें। res.status(404).json(...) जैसा Chaining common है।

Note: res.json आपके लिए Content-Type header set करता है।

उदाहरण: Request/Response

bash
app.get("/search", (req, res) => {
  const { q = "", page = "1" } = req.query;
  if (!q) return res.status(400).json({ error: "q is required" });
  res.json({ q, page: Number(page), agent: req.get("user-agent") });
});
app.get("/old", (req, res) => res.redirect(301, "/search?q=new"));

⚠️ 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. एक response दो बार भेजना
  2. body parser middleware भूल जाना
  3. status set न करना
चैप्टर सारांश
  • req.params, query, और body input carry करते हैं
  • res.json और res.send respond करते हैं
  • res.status code set करता है
  • Helpers chain करें
🔒

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.