SORU
14 Ocak 2011, Cuma


node.js expressjs res ve talep nedir?

Örnek express.js :

app.get('/user/:id', function(req, res){
    res.send('user '   req.params.id);
});

Ger ve res nedir? Onlar ne yaparlar, ne demek ve ne yapıyorlar?

CEVAP
14 Ocak 2011, Cuma


req bir nesne, olay ortaya bir HTTP isteği hakkında bilgi içeren. req T res istenilen HTTP yanıtı geri göndermek için kullanın.

Bu parametreler adlandırılabilir. Eğer daha net olsaydı bu kodu değiştirebilir:

app.get('/user/:id', function(request, response){
  response.send('user '   request.params.id);
});

Düzenleme:

Bu yöntem var ki:

app.get('/people.json', function(request, response) { });

İsteği bu (sadece birkaç isim) gibi özelliklere sahip bir nesne olacak:

  • Olacak request.url, "/insanlar.json bir eylemi, tetiklendiğinde"
  • "" Bu durumda, app.get() Ara. dolayısıyla ELDE edilecek* *11,
  • HTTP bir dizi request.headers, `istek gibi öğeleri içeren başlık.başlıkları.'ya da HTTP sıkıştırma anlamak mümkün, vb. olup olmadığını işleyebilir ne isteği, ne belirlemek için kullanabileceğiniz kabul
  • Varsa request.params (örneğin, /insanlar.eğer orada sorgu dizesi parametreleri bir dizi json?foo=bar dizesini içeren request.params.foo neden"") bar.

Bu taleplere cevap vermek için response nesnesi yanıtınız oluşturmak için kullanın. İnsanlar üzerinde genişletmek için.örnek 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 would probably come 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);
});

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • dope2111

    dope2111

    29 HAZİRAN 2009
  • erikbjgn's channel

    erikbjgn's c

    12 Mayıs 2008
  • Kyler Briskey

    Kyler Briske

    20 ŞUBAT 2011