Request और Response
Express request और response objects में friendly helpers add करता है।
In this page:
Syntax
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
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. #}
आम गलतियां
- एक response दो बार भेजना
- body parser middleware भूल जाना
- 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: