-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (52 loc) · 1.24 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
/**
* Dependencies
*/
const app = module.exports = require('express')();
const qs = require('querystring');
app.set('view engine', 'ejs');
app.get('/', function(req, res, next) {
var url = req.query.url;
// everything breaks if we don't have a url parameter
if (!url) return next(new Error('requires `url` parameter'));
res.render('index', {
url: req.query.url,
query: qs.stringify(req.query)
});
});
app.get('/oembed.json', function(req, res) {
var params = req.query;
var json = Object.assign({
type: 'rich',
width: 300,
height: 600,
html: `<iframe border="0" src="${params.url}"/>`
}, params);
coerce(json, [
'width',
'height',
'thumbnail_width',
'thumbnail_height',
'cache_age'
], Number)
res.json(json);
});
// hit when no route is matched
app.use(function(req, res) {
res.status(404);
res.send(`Error 404: "page not found"`);
});
// hit when `next(new Error())`
app.use(function(err, req, res, next) { // eslint-disable-line
res.status(500);
res.send(`Error 500: "${err.message}"`);
});
/**
* Utils
*/
function coerce(object, keys, type) {
keys.forEach(key => {
var value = object[key];
if (value === undefined) return;
object[key] = type(value);
});
}