निम्नलिखित एक्सप्रेस समारोह में:
app.get('/user/:id', function(req, res){
res.send('user' + req.params.id);
});
क्या हैं reqऔर res? वे किस लिए खड़े हैं, उनका क्या मतलब है, और वे क्या करते हैं?
धन्यवाद!
निम्नलिखित एक्सप्रेस समारोह में:
app.get('/user/:id', function(req, res){
res.send('user' + req.params.id);
});
क्या हैं reqऔर res? वे किस लिए खड़े हैं, उनका क्या मतलब है, और वे क्या करते हैं?
धन्यवाद!
जवाबों:
reqघटना को बढ़ाने वाले HTTP अनुरोध के बारे में जानकारी वाली एक वस्तु है। के जवाब में req, आप resवांछित HTTP प्रतिक्रिया को वापस भेजने के लिए उपयोग करते हैं।
उन मापदंडों को कुछ भी नाम दिया जा सकता है। अधिक स्पष्ट होने पर आप इसे उस कोड में बदल सकते हैं:
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
संपादित करें:
कहो कि आपके पास यह विधि है:
app.get('/people.json', function(request, response) { });
अनुरोध इन जैसे गुणों के साथ एक वस्तु होगी (केवल कुछ का नाम देने के लिए):
request.url, जो "/people.json"तब होगा जब यह विशेष कार्रवाई शुरू हो जाएगीrequest.method, जो "GET"इस मामले में होगा , इसलिए app.get()कॉल।request.headers, जिसमें आइटम हैं request.headers.accept, जिसका उपयोग आप यह निर्धारित करने के लिए कर सकते हैं कि किस प्रकार के ब्राउज़र ने अनुरोध किया है, यह किस प्रकार की प्रतिक्रियाओं को संभाल सकता है, क्या यह HTTP संपीड़न, आदि को समझने में सक्षम है या नहीं।request.query(जैसे कि स्ट्रिंग /people.json?foo=barको request.query.fooशामिल करने में परिणाम होगा "bar")।उस अनुरोध का जवाब देने के लिए, आप अपनी प्रतिक्रिया बनाने के लिए प्रतिक्रिया वस्तु का उपयोग करते हैं। people.jsonउदाहरण पर विस्तार करने के लिए:
app.get('/people.json', function(request, response) {
// We want to set the content-type header so that the browser understands
// the content of the response.
response.contentType('application/json');
// Normally, the data is fetched from a database, but we can cheat:
var people = [
{ name: 'Dave', location: 'Atlanta' },
{ name: 'Santa Claus', location: 'North Pole' },
{ name: 'Man in the Moon', location: 'The Moon' }
];
// Since the request is for a JSON representation of the people, we
// should JSON serialize them. The built-in JSON.stringify() function
// does that.
var peopleJSON = JSON.stringify(people);
// Now, we can use the response object's send method to push that string
// of people JSON back to the browser in response to this request:
response.send(peopleJSON);
});
reqऔर resसंरचना, यह एक्सप्रेस डॉक्स में वर्णित है: req: expressjs.com/en/api.html#req , res: expressjs.com/en/api.html#res
मुझे डेव वार्ड के उत्तर में एक त्रुटि (शायद एक हालिया परिवर्तन?): क्वेरी स्ट्रिंग पैरामाटर्स में हैं request.query, नहीं request.params। (देखें https://stackoverflow.com/a/6913287/166530 )
request.params डिफ़ॉल्ट रूप से मार्गों में किसी भी "घटक मिलान" के मूल्य से भरा होता है, अर्थात
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
और, यदि आपने app.use(express.bodyParser());POST'ed formdata के साथ इसके बॉडीप्रेसर ( ) का उपयोग करने के लिए एक्सप्रेस को कॉन्फ़िगर किया है । (देखें कि POST क्वेरी पैरामीटर कैसे प्राप्त करें? )
req=="request"//res=="response"