从API获取需要If-modified-since的数据,在节点js中使用request.js
我正在做一个4chan API的代理。 我在Node.js + Express中使用request.js来查询API,我不知道如何完全实现API所需的“If-modified-since”,这是代码:
app.get('/api/boards', function(request, response){ req({uri:'https://api.4chan.org/boards.json', json: true}, function (error, res, data) { if (!error && res.statusCode == 200) { response.jsonp(data['boards']); } }); });
如果我对已经完成的4chan进行查询,它不会回答,超时会触发。
4chan API规则:
- 每秒不要提出一个以上的请求。
- 线程更新应该设置为最less10秒,最好是更高。
- 在做请求时使用If-Modified-Since。
- 使用与应用程序相同的协议来制作API请求。 只有当用户通过HTTPS访问您的应用程序时才使用SSL。
- 稍后再来…
请求模块允许您将请求标头传递给选项,所以你可以这样做:
var request = require('request'); var options = { uri: 'https://api.4chan.org/boards.json', json: true, headers: {'If-Modified-Since': 'Sun, 06 Oct 2013 01:16:45 GMT'} }; request(options, function (error, res, data) { // other processing });