javascript - 从需要 If-modified-since 的 API 中获取数据,在 node,js 中使用 request.js

标签 javascript node.js http-status-code-304

我正在为 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/

相关文章:

node.js - Bluebird 而不是 Koa 中的 Co?

http - 为什么304状态码算作 "redirect?"

php - 我是否必须再次发送带有 304 Not Modified header 的 ETag?

javascript - 关于requirejs异步

node.js - FFmpeg流动态png

javascript - 如何在 React 中测试类组件

javascript - 将 View 渲染为 HTML

caching - 使用 maxage header 时不可能出现 304 响应

javascript - React中箭头函数的正确使用

javascript - DOM 元素可以有一个值为任意对象(不是字符串)的属性吗?