2016-06-12 6 views
0

node.jsで動作するように単純なhttpリクエストを受け取っていないようです。次のコードは、何もロギングせずにエラーを出力するだけです。node.js httpモジュールを使用しています

http = require 'http' 

callback = (response) -> 
    console.log 'callback' 
    str = '' 

    # another chunk of data has been recieved, so append it to `str` 
    response.on 'data', (chunk) -> 
    console.log 'data' 
    str += chunk 

    # the whole response has been recieved, so we just print it out here 
    response.on 'end', -> 
    console.log str 

http.get 'http://hacker-news.firebaseio.com/v0/topstories.json', callback 

生成されるJavaScript:

// Generated by CoffeeScript 1.10.0 
(function() { 
    var callback, http; 

    http = require('http'); 

    callback = function(response) { 
    var str; 
    console.log('callback'); 
    str = ''; 
    response.on('data', function(chunk) { 
     console.log('data'); 
     return str += chunk; 
    }); 
    return response.on('end', function() { 
     return console.log(str); 
    }); 
    }; 

    http.get('http://hacker-news.firebaseio.com/v0/topstories.json', callback); 

}).call(this); 

答えて

0

あなたの既存のコードは、他のURLで正常に動作するようです。そのURLは特にHTTP経由では動作しないようですが、少なくともcurlを使用するとハングします。ブラウザにロードすると、HTTPSバージョンに307リダイレクトされたように見えるので、なぜそれがcurlまたはあなたのコードに応答していないのか分かりません。

にかかわらず、あなたはおそらくこれだけのことを行うようにコードを変更し、とにかくHTTPS URLを使用する:

https = require 'https' 

callback = (response) -> 
    console.log 'callback' 
    str = '' 

    # another chunk of data has been recieved, so append it to `str` 
    response.on 'data', (chunk) -> 
    console.log 'data' 
    str += chunk 

    # the whole response has been recieved, so we just print it out here 
    response.on 'end', -> 
    console.log str 

https.get 'https://hacker-news.firebaseio.com/v0/topstories.json', callback 
0

、「要求」や「要求などのHTTPとHTTPSの両方をサポートするTry使用libに、 -約束する'。以下の例は、正常に動作します

var request = require('request'); 
request('https://www.xxxxx.com', function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
    console.log(body); 
    } 
}) 

詳細情報:https://www.npmjs.com/package/request

関連する問題