forked from serviceworker/offline-news-service-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (38 loc) · 1.11 KB
/
index.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
var port = Number(process.env.PORT || 8080);
var api = 'http' + (port === 8080 ? '://localhost:3000' : 's://offline-news-api.herokuapp.com') + '/stories';
var express = require('express');
var path = require('path');
var request = require('superagent');
var templates = require('./public/templates');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/article/:guid', function(req, res) {
request.get(api+'/'+req.params.guid)
.end(function(err, data) {
var article = data.body;
if (err || !data.ok) {
res.status(404);
res.send(templates.article({
title: 'Story cannot be found',
body: '<p>Please try another</p>'
}));
} else {
res.send(templates.article({
title: article.title,
body: article.body
}));
}
});
});
app.get('/', function(req, res) {
request.get(api)
.end(function(err, data) {
if (err) {
res.status(404).end();
} else {
res.send(templates.list(data.body));
}
});
});
app.listen(port);
console.log('listening on '+port);