-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·86 lines (77 loc) · 2.18 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Module dependencies.
*/
var prismic = require('prismic-nodejs');
var app = require('./config');
var configuration = require('./prismic-configuration');
var PORT = app.get('port');
// Returns a Promise
function api(req, res) {
// So we can use this information in the views
res.locals.ctx = {
endpoint: configuration.apiEndpoint,
linkResolver: configuration.linkResolver,
dateConverter: configuration.dateConverter
};
return prismic.api(configuration.apiEndpoint, {
accessToken: configuration.accessToken,
req: req
});
}
function handleError(err, req, res) {
if (err.status == 404) {
res.status(404).send("404 not found");
} else {
res.status(500).send("Error 500: " + err.message);
}
}
app.listen(PORT, function() {
console.log('Express server listening on port ' + PORT);
});
app.route('/preview').get(function(req, res) {
api(req, res).then(function(api) {
return prismic.preview(api, configuration.linkResolver, req, res);
}).catch(function(err) {
handleError(err, req, res);
});
});
app.route('/:uid').get(function(req, res) {
var uid = req.params.uid;
api(req, res).then(function(api) {
api.getSingle("site-layout").then(function(layout) {
api.getByUID('article', uid).then(function(pageContent) {
if(pageContent.uid != uid){
return res.redirect("/"+pageContent.uid);
}
res.render('article', {
layout: layout,
pageContent: pageContent
});
}).catch(function(err) {
handleError(err, req, res);
});
}).catch(function(err) {
handleError(err, req, res);
});
});
});
// Route for homepage
app.route('/').get(function(req, res) {
api(req, res).then(function(api) {
api.getSingle("site-layout").then(function(layout) {
api.query(
prismic.Predicates.at("document.type", "article"),
{ orderings: '[my.article.date desc]' }
).then(function(articles) {
res.render('homepage', {
layout: layout,
articles: articles.results
});
}).catch(function(err) {
handleError(err, req, res);
});
}).catch(function(err) {
handleError(err, req, res);
});
});
});