-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (47 loc) · 1.43 KB
/
server.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
var express = require('express');
var https = require('https');
var compress = require('compression');
var cors = require('cors');
var bodyParser = require('body-parser');
var app = express();
app.use(errorHandler);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(compress());
app.use(cors());
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.redirect('index.html');
});
app.get('/pics', function(req, res) {
var flickrUrl = 'https://www.flickr.com/services/feeds/photos_public.gne/?format=json&jsoncallback=?';
if (req.query.tags) {
flickrUrl += '&tags=' + req.query.tags;
}
https.get(flickrUrl, function(httpResponse) {
var data = "";
res.setHeader('Content-Type', 'application/json');
httpResponse.on('data', function(chunk) {
data += chunk;
});
httpResponse.on('end', function(chunk) {
data = "jsonpToJson" + data; //hack to extract the value in json format
var json = eval(data);
res.send(json);
});
}).on('error', function(err) {
res.status(500).send(err);
});
});
function jsonpToJson(json) {
return json;
}
var port = process.env.PORT || 5000;
app.listen(port, '0.0.0.0', function(err) {
if (err) return console.err('Error: ', err.stack);
console.log('Listening on port ' + port);
});
function errorHandler(err, req, res, next) {
console.error('Service error: ', err.stack);
next();
}