我正在为 4chan API. 做一个代理我正在使用 request.js在 Node.js + Express 中对 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 规则:
- Do not make more than one request per second.
- Thread updating should be set to a minimum of 10 seconds, preferably higher.
- Use If-Modified-Since when doing your requests.
- Make API requests using the same protocol as the app. Only use SSL when a user is accessing your app over HTTPS.
- More to come later...
最佳答案
请求模块允许您将请求 header 传递到选项中,因此您可以这样做:
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
});
关于javascript - 从需要 If-modified-since 的 API 中获取数据,在 node,js 中使用 request.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19204054/