-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (51 loc) · 1.51 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
53
54
55
var express = require('express');
var Promise = require('bluebird');
var _ = require('lodash');
var app = express();
var validator = require('amp-validator');
var exphbs = require('express-handlebars');
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static('dist'));
app.use(express.static('assets'));
app.get('/', function (req, res) {
console.log('rendering homepage');
res.render('index');
});
app.get('/validate', function (req, res) {
var urls = [].concat(req.query.url);
Promise.all(_.map(_.compact(urls), function (url) {
if (url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0) {
url = 'http://' + url;
}
console.log('validating', url);
return validator.validate(url).then(function (result) {
if (result.ampVersion.declared === 'none' && result.errors.length === 0) {
result.errors = [
{
reason: 'This doesn\'t seem to be an AMP document'
}
];
}
return _.extend({
url: url
}, result);
}).catch(function (err) {
console.error(err);
return {
url: url,
errors: [{
reason: err
}]
};
});
})).then(function (results) {
console.log('rendering validation results');
console.log('results', JSON.stringify(results));
res.render('index', {results: results});
}).catch(function (err) {
console.error(err);
res.redirect('/');
}).done();
});
app.listen(process.env.PORT || 7805);