Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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...
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
});
share|improve this answer
    
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 7 '13 at 20:55
    
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 7 '13 at 21:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.