-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (56 loc) · 1.87 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require( 'express' );
const fastXmlParser = require( 'fast-xml-parser' );
const rp = require( 'request-promise' );
const http = require( 'http' );
const path = require( 'path' );
const app = express();
const FLICKR_URL = 'https://api.flickr.com/services/feeds/photos_public.gne?format=rest&lang=en-us';
app.use( function ( req, res, next ) {
res.header( "Access-Control-Allow-Origin", "*" );
res.header( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" );
next();
} );
app.use( express.static( path.join( __dirname, 'dist') ) );
app.get( '/feeds', async ( req, res ) => {
const data = await rp( FLICKR_URL );
const response = fastXmlParser.parse( data, {
attributeNamePrefix: '$',
ignoreAttributes: false
} );
if ( response.feed && response.feed.entry ) {
const result = response.feed.entry.map( entry => {
return {
title: entry.title,
img: getEnclosureLink( entry.link ),
published: entry.published,
author: entry.author,
category: getCategories( entry.category ),
}
} );
res.status( 200 ).json( result );
} else {
res.status( 500 ).json( { error: 'No feed or entries available on url provided' } );
}
} );
function getEnclosureLink( links ) {
return links
.filter( link => link.$rel === 'enclosure' )
.pop()
.$href
;
}
function getCategories( data ) {
const categories = data instanceof ( Array ) ? data : [ data ];
return categories
.map( val => val.$term )
.filter( val => val !== '' )
;
}
app.get( '*', ( req, res ) => {
res.sendfile( path.join( __dirname, 'dist/index.html' ) );
} );
const port = process.env.PORT || 3001;
app.set( 'port', port );
const server = http.createServer( app );
server.listen( port, () => console.log( 'running' ) );
// app.listen( 3000, () => console.log( 'Api listening on port 3000' ) );