@@ -22,6 +22,7 @@ const memCache = require('memory-cache');
22
22
const pubBackend = require ( './Backend.js' ) ;
23
23
const enforce = require ( 'express-sslify' ) ;
24
24
const helmet = require ( 'helmet' ) ;
25
+ const xmlParser = require ( 'xml2json' ) ;
25
26
26
27
const ENVIRONMENT_PRODUCTION = 'production' ;
27
28
@@ -38,21 +39,20 @@ if (app.get('env') === ENVIRONMENT_PRODUCTION) {
38
39
39
40
// how long (in seconds) to cache requests for main feed and for any article
40
41
const cacheDurations = { feed : 600 , article : 3600 } ;
41
- const feedURL = 'https://query.yahooapis.com/v1/public/yql' ;
42
42
43
43
const staticFilesMiddleware = express . static ( 'dist' ) ;
44
44
app . use ( staticFilesMiddleware ) ;
45
45
46
- // Proxy the feed request so that we can cache it.
47
- // YQL expects a query in the "q" parameter.
46
+ // Here, we cache the feed, and we convert it to JSON.
48
47
app . get ( '/feed' , cache ( cacheDurations . feed ) , function ( req , res , next ) {
49
48
const options = {
50
- url : feedURL + '?format=json&q=' + req . query . q
49
+ url : req . query . q
51
50
} ;
52
51
52
+
53
53
request ( options , ( error , response , body ) => {
54
54
if ( ! error ) {
55
- res . send ( body ) ;
55
+ res . send ( xmlParser . toJson ( body ) ) ;
56
56
} else {
57
57
res . json ( { error : 'An error occurred in /feed route' } ) ;
58
58
}
0 commit comments