-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
132 lines (102 loc) · 3.04 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
const express = require('express');
const butter = require('buttercms')('e389441bfb10ccd3228e5adb47ccb98a8183eca7');
const app = express()
app.use(express.static('public'))
app.set('view engine', 'ejs');
// Routes
app.get('/', renderHome)
app.get('/blog', renderBlogHome)
app.get('/blog/:slug', renderPost)
app.get('/category/:slug', renderCategory)
app.get('/author/:slug', renderAuthor)
app.get('/rss', renderRss)
app.get('/atom', renderAtom)
app.get('/sitemap', renderSitemap)
// Location Pages in Butter
app.get('/locations/:slug', renderPage)
// Start server
app.listen(process.env.PORT || 3000)
function renderPage(req, res) {
var slug = req.params.slug || 1;
butter.content.retrieve(['location_pages[slug='+slug+']']).then(function(resp) {
var content = resp.data.data;
// location_pages is a collection so we access the first and only item
var location = content.location_pages[0];
res.render('location', {
location: location
})
});
}
function renderHome(req, res) {
// Display list of location pages on our homepage.
butter.content.retrieve(['location_pages']).then(function(resp) {
var content = resp.data.data;
var location_pages = content.location_pages;
butter.post.list({page_size: 4, page: 1}).then(function(postResp) {
var posts = postResp.data.data;
res.render('index', {
locations: location_pages,
posts: posts
})
});
});
}
function renderBlogHome(req, res) {
var page = req.params.page || 1;
butter.post.list({page_size: 10, page: page}).then(function(resp) {
res.render('bloghome', {
posts: resp.data.data,
next_page: resp.data.meta.next_page,
previous_page: resp.data.meta.previous_page
})
})
}
function renderPost(req, res) {
var slug = req.params.slug;
butter.post.retrieve(slug).then(function(resp) {
res.render('post', {
title: resp.data.data.title,
post: resp.data.data,
published: new Date(resp.data.data.published)
})
})
}
function renderAuthor(req, res) {
var slug = req.params.slug;
butter.author.retrieve(slug, {include: 'recent_posts'}).
then(function(resp) {
res.render('author', {
title: `${resp.data.data.first_name} ${resp.data.data.last_name}`,
author: resp.data.data
})
})
}
function renderCategory(req, res) {
var slug = req.params.slug;
butter.category.retrieve(slug, {include: 'recent_posts'})
.then(function(resp) {
res.render('category', {
title: resp.data.data.name,
category: resp.data.data
})
})
}
function renderAtom(req, res) {
res.set('Content-Type', 'text/xml');
butter.feed.retrieve('atom').then(function(resp) {
res.send(resp.data.data)
})
}
function renderRss(req, res) {
res.set('Content-Type', 'text/xml');
butter.feed.retrieve('rss').then(function(resp) {
res.send(resp.data.data)
})
}
function renderSitemap(req, res) {
res.set('Content-Type', 'text/xml');
butter.feed.retrieve('sitemap').then(function(resp) {
res.send(resp.data.data)
})
}