3

I'm making a proxy to the 4chan API. I'm using request.js in Node.js + Express to make the queries to the API and I don't know how exactly implement the "If-modified-since" that the API requires, this is the code:

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']);
        }
    });
});

If I make a query to 4chan that already has been done it doesn't answer and the timeout fires.

4chan API rules:

  • 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...
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
nbreath
  • 100
  • 2
  • 9

1 Answers1

4

The request module allows you to pass request headers into the options, so you can do this:

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
});
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • Thanks! Another question, now I added the header and the API responds always, not blocking me the access but because is with a 304 code no new data arrives and I send nothing in the http response. How I can make a 200 connection save the data and then when I get a 304 respond with that data. How will be the correct way to implement this? – nbreath Oct 07 '13 at 20:55
  • 1
    There's multiple ways to do this - if there isn't too much data, then you can store the data into a variable. Otherwise, you could write the data into a file and access it later. – hexacyanide Oct 07 '13 at 21:32