0

I have two URLs that have a .json.gz file -

var url = "http://post.s3post.cf/s3posts.json.gz";
var backupURL = "https://s3-us-west-2.amazonaws.com/s3post.cf/s3posts.json.gz";

I'm able to successfully use the request module to get json from the file -

app.all('*', function(req, res, next) {
    request({
        method: 'GET',
        uri: url,
        gzip: true
    }, function(error, response, body) {
        res.locals.posts = JSON.parse(body);
        next();
    });
});

What I want to do is, if the request with url fails, I want to use the backupURL to get the json from. So logically, I thought if I received an error, I would use a nested request to do this -

app.all('*', function(req, res, next) {
    request({
        method: 'GET',
        uri: url,
        gzip: true
    }, function(error, response, body) {
        if(error) {
            request({
                method: 'GET',
                uri: backupURL,
                gzip: true
            }, function(error, response, body) {
                res.locals.posts = JSON.parse(body);
                next();
            });
        } else {
            res.locals.posts = JSON.parse(body);
            next();
        }
    });
});

This is not working. Individually, both the URLs work with one request. What can I do to use backupURL when the request with url fails?

EDIT 1 -

The program compiles and starts listening to my app. It is when I request a page that it crashes with this error -

undefined:1
<?xml version="1.0" encoding="UTF-8"?>
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at Request._callback (/Users/Anish/Workspace/NodeJS/unzipper/app.js:65:28)
    at Request.self.callback (/Users/Anish/Workspace/NodeJS/unzipper/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:194:7)
    at Request.<anonymous> (/Users/Anish/Workspace/NodeJS/unzipper/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:191:7)
    at IncomingMessage.<anonymous> (/Users/Anish/Workspace/NodeJS/unzipper/node_modules/request/request.js:1091:12)
    at Object.onceWrapper (events.js:293:19)
Anish Sana
  • 518
  • 1
  • 12
  • 30
  • 1
    'Not working' is not a problem description. There are consequences, detail those. Namely any error that is shown. You can also employ some poor-mans-debugging and add console.log statements to see exactly which path the code takes and what the content of variables is at that point. – Gimby Apr 11 '17 at 11:36
  • @Gimby I have edited my question with the error information. – Anish Sana Apr 11 '17 at 11:42

2 Answers2

1

try to use response.status if its is 200 then move further else if its 400 or 500 then use backup URL

Vipul Pandey
  • 1,507
  • 11
  • 20
1

It's parsing error.

< it seems like you are getting HTML data and at first position you are getting parsing error

Always check your body before parse

if(typeof body==='string'){
   body = JSON.parse(body);
}
res.locals.posts = body;
abdulbarik
  • 6,101
  • 5
  • 38
  • 59