forked from chubas/instaband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (50 loc) · 1.3 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
var Article = require('./lib/wikipedia');
var Quote = require('./lib/wikiquote');
var Photo = require('./lib/flickr');
var express = require('express');
var app = express();
var http = require('http');
var url = require('url');
app.use(express.static(__dirname + '/public'));
var respondWithRandom = function(resource, res) {
resource.getRandom()
.then(function(result) {
res.json(result);
})
.catch(function() {
res.status(500).send("Error fetching " + resource.name);
});
};
app.get('/photo', function(req, res) {
respondWithRandom(Photo, res);
});
app.get('/quote', function(req, res) {
respondWithRandom(Quote, res);
});
app.get('/article', function(req, res) {
respondWithRandom(Article, res);
});
exports.startServer = function(cb) {
app.listen(process.env.PORT || 3333);
cb();
};
app.get('/proxy', function(req, res) {
var proxied = req.query.url;
var urlParts = url.parse(proxied, true);
var options = {
host: urlParts.host,
path: urlParts.path
};
var callback = function(response) {
if (response.statusCode === 200) {
res.writeHead(200, {
'Content-Type': response.headers['content-type']
});
response.pipe(res);
} else {
res.writeHead(response.statusCode);
res.end();
}
};
http.request(options, callback).end();
});