SORU
4 NİSAN 2012, ÇARŞAMBA


Nasıl bir Ekspres uygulamasında JSON POST veri tüketmek

Benim sunucu için aşağıdaki JSON dize gönderiyorum.

(
        {
        id = 1;
        name = foo;
    },
        {
        id = 2;
        name = bar;
    }
)

Sunucu üzerinde bu var.

app.post('/', function(request, response) {

    console.log("Got response: "   response.statusCode);

    response.on('data', function(chunk) {
        queryResponse =chunk;
        console.log('data');
    });

    response.on('end', function(){
        console.log('end');
    });
});

Dize gönderdiğim zaman, 200 cevap var gösteriyor, ama diğer iki yöntem asla çalıştırın. Bu yüzden mi?

CEVAP
4 NİSAN 2012, ÇARŞAMBA


request ile response nesne kullanımı üstünde olduğunu düşünüyorum.

response nesne request vücut erişim için arayanlar ise HTTP yanıt arayan istemciye geri göndermek için. Bu bazı rehberlik sağlar answer bkz.

Kullanıyorsanız geçerli JSON ve kayıt ile Content-Type: application/json, daha sonra kullanabilirsiniz bodyParser katman için ayrıştırma isteği vücut ve sonuç request.body rotanız.

var express = require('express')
  , app = express.createServer();

app.use(express.bodyParser());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
  response.send(request.body);    // echo the result back
});

app.listen(3000);

Birlikte hatları Test:

$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
{"MyKey":"My Value"}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • emimusic

    emimusic

    10 Mart 2006
  • IGN

    IGN

    19 EYLÜL 2006
  • Random Act of Kindness Video Contest

    Random Act o

    18 EYLÜL 2012