2014-03-31 56 views
0

documentation of the Github API之后创建NodeJS应用程序的授权。408请求Github API的NodeJS应用程序超时

我有以下代码:

var _options = { 
    headers: { 
    'User-Agent': app.get('ORGANISATION') 
    }, 
    hostname: 'api.github.com' 
}; 

var oauth2Authorize = function() { 
    var path = '/authorizations?scopes=repo'; 
    path += '&client_id='+ app.get('GITHUB_CLIENT_ID'); 
    path += '&client_secret='+ app.get('GITHUB_CLIENT_SECRET'); 
    path += '&note=ReviewerAssistant'; 

    _options.path = path; 
    _options.method = 'POST'; 

    var request = https.request(_options, function (response) { 
    var data = ""; 

    response.on('data', function (chunk) { 
     data += chunk; 
    }); 

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

    request.on('error', function (error) { 
    console.log('Problem with request: '+ error); 
    }); 
}; 

和所有我得到的是:

408 Request Time-out 
Your browser didn't send a complete request in time. 

做一个GET要求工作虽然。

回答

2

http.request() doesn't immediately send the request

随着http.request()必须始终调用req.end()以表示你正在使用的要求做 - 即使没有数据被写入请求主体。

它打开到服务器的底层连接,但保留request不完全,使身体/消息可以用它来发送:

var request = http.request({ method: 'POST', ... }); 

request.write('data\n'); 
request.write('data\n'); 
request.end(); 

而且,无论有什么write()或不,您必须致电end()完成request并将其发送完整。没有这一点,服务器将最终强制开放连接关闭。在这种情况下,用408响应。

+0

我是个白痴。我在2周前阅读了这篇文章,并且忘记了......谢谢! –