2013-08-05 14 views
0

Server1.js如何獲取Node.js中的發佈數據?

var data = querystring.stringify({ 
    imageName: reqImgName 
    }); 

var options = { 
       host: 'localhost', 
       port: 4321, 
       path: '/image', 
       method: 'POST', 
       headers: { 
        'Content-Type': 'application/x-www-form-urlencoded', 
        'Content-Length': data.length 
       } 
      }; 

server2.js

http.createServer(function(req, res){ 
    var reqMethod=req.method; 
    var request = url.parse(req.url, true); 
    var pathName = request.pathname; 
    console.log('Path name is '+pathName); 
    if (reqMethod=='POST' && pathName == '/image') { 

    //here i need my server1 data..how can i get here. 
    } 

}).listen(4321); 

回答

5
var postData = ''; 
req.on('data', function(datum) { 
    postData += datum; 
}); 

req.on('end', function() { 
    //read postData 
}); 

您沒有收到任何發佈數據,因爲您沒有在server1.js中發送任何數據。嘗試一些數據寫入請求主體

var req = http.request(options, function(res) { 

}); 


req.write('data=somedata'); 

調試服務器2的另一種方法是讓瀏覽器發起POST請求/圖像

+0

我已經嘗試了本but..but IM在POSTDATA沒有得到任何東西 – sachin

+0

請檢查更新後的帖子。 –

+0

謝謝你..以前我發送過這樣的req.write('data');那是我沒有得到圖像名稱..現在工作很好.. – sachin

1

把事件偵聽器的reqdataend事件。 data會給你大量的數據,你可以逐步處理,並且end會告訴你什麼時候你擁有了所有的東西。

相關問題