我有两个有.json.gz
文件的 URL -
var url = "http://post.s3post.cf/s3posts.json.gz";
var backupURL = "https://s3-us-west-2.amazonaws.com/s3post.cf/s3posts.json.gz";
我能够成功使用请求模块从文件中获取json
-
app.all('*', function(req, res, next) {
request({
method: 'GET',
uri: url,
gzip: true
}, function(error, response, body) {
res.locals.posts = JSON.parse(body);
next();
});
});
我想要做的是,如果请求url
失败,我想使用backupURL
来获取json
from。所以从逻辑上讲,我想如果我收到错误,我会使用嵌套请求来执行此操作 -
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();
}
});
});
这是行不通的。单独地,这两个 URL 都适用于一个请求。backupURL
当请求url
失败时我可以做什么?
编辑 1 -
该程序编译并开始收听我的应用程序。当我请求一个页面时,它会因此错误而崩溃 -
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)