-1
我在從http.request獲取節點js的數據時遇到問題我在控制檯中獲取數據,但是當我嘗試從函數中獲取它時,它並沒有進入變量,這裏是代碼,當我試圖發送端口請求時轉發我在x回調函數中得到響應,但沒有找到數據,所以任何知道的人都請讓我知道。如何從節點JS中的回調函數中獲取數據?
var fs = require('fs');
var http = require('http');
var url = require('url') ,
httpProxy = require('http-proxy');
var express = require("express");
//
// Create your proxy server
//
httpProxy.createServer(9000, 'localhost').listen(8000);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
//res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
var queryObject = url.parse(req.url,true).query;
res.writeHead(200);
if(queryObject['id']!==undefined){
console.log(queryObject);
//alert(queryObject['id']);
if(queryObject['id'].match('100'))
{
res.write(queryObject['id']+" forwarding to another port 8000");
//sending request
var options = {
host: '192.168.10.33',
port: 8080,
path: '/demo.js?id='+queryObject['id'],
method: 'GET',
headers: {
accept: 'text/plain'
}
};
console.log("Start conecting ...");
var x = http.request(options,function(res2){
console.log("Connected.");
res2.on('data',function(data){
//*********
//Here is the problem occur this data i m cloud not able to print
//********
console.log("data-> "+data);
});
});
x.end("\n ok");
//end
}
else
{
res.write("listening on current port");
}
}
res.end("\n end of page ");//'\n\nFeel free to add query parameters to the end of the url');
//res.end();
}).listen(9000);