如何使用nodejs进行子HTTP请求

我想知道如何使用HTTP请求或使用nodejs的某些lib来使第二个请求享受第一个请求(外部URL)的会话。 下面是我如何解决我的问题与PHP和cURL,我在哪里访问主要的url,我把会议(cookies)作出第二个要求

$app->get('/parada/:termo', function ($termo) use ($app) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://olhovivo.sptrans.com.br/'); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec ($ch); curl_setopt($ch, CURLOPT_URL, 'http://olhovivo.sptrans.com.br/v0/Parada/Buscar?termosBusca='.$termo); $content = curl_exec ($ch); echo $content; curl_close ($ch); }); 

我没有代码,因为所有的例子,我已经看到了http.get作出申请,并closures连接。

 var http = require('http'); var options = { host: 'example.com', port: 80, path: '/foo.html' }; http.get(options, function(resp){ resp.on('data', function(chunk){ //new request?? }); }).on("error", function(e){ console.log("Got error: " + e.message); }); 

我需要通过http.get导航,因为我需要捕获第一个URL的cookie能够validation第二个url。 与PHP cURL很简单,因为只需要closures第一个连接,使第二个请求(保持cookie)


我试图抓住cookie的第一个请求,然后我试图在第二个请求中插入cookie的第一个请求,但没有运行

 var http = require('http'); var options = { host: 'www.olhovivo.sptrans.com.br', port: 80 }; http.get(options, function(resp){ var teste = resp.headers; teste = teste['set-cookie'][0].split('='); var cok = 'apiCredentials-v0='+teste[1].replace('; path', ''); resp.on('data', function(chunk){ //new request?? var options2 = { host: 'www.olhovivo.sptrans.com.br', port: 80, "set-cookie": cok, path: '/v0/Parada/Buscar?termosBusca=Paulista' }; http.get(options2, function(resp2){ resp2.on('data', function(chunk2){ //new request?? http.createServer(function (req, res) { //set content header res.writeHead(200, { 'content-type': 'application/json' }); //write msg res.end(chunk2); }).listen(8080); console.log('Server running 0n 8080'); }); }).on("error", function(e){ console.log("Got error: " + e.message); }); }); }).on("error", function(e){ console.log("Got error: " + e.message); }); 

在下面的例子中,我发出第一个请求来获取cookie apiCredentials 。 我将这个cookie添加到第二个请求的标题。 第二个请求从提供程序返回正确的数据(一个不错的JSON答案)。

 var http = require("http"); var initialRequestOptions = { host : 'olhovivo.sptrans.com.br', port : 80, path : '/' }; var secondRequestOptions = { host : 'olhovivo.sptrans.com.br', port : 80, path : '/v0/Parada/Buscar?termosBusca=Morato', headers : { // This cookie will be replaced by the one received on the previous request 'Cookie' : 'apiCredentials=39D1DE24EB4A....' } }; var firstRequest = http.request(initialRequestOptions, function(response) { var cookie = response.headers['set-cookie']; if (cookie) { cookie = (cookie + '').split(";")[0]; secondRequestOptions.headers['Cookie'] = cookie; } var body = ''; response.on('data', function(chunk) { body += chunk; }); response.on('end', function() { var secondRequest = http.request(secondRequestOptions, function(responseNew) { var dataBody = ''; responseNew.on('data', function(chunk) { dataBody += chunk; }); responseNew.on('end', function() { console.log(dataBody); }); }); secondRequest.on('error', function(e) { console.log('problem with request: ' + e.message); }); secondRequest.end(); }); }); firstRequest.on('error', function(e) { console.log('problem with request: ' + e.message); }); firstRequest.end(); 

我build议使用请求模块。

 var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } })