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
|
'use strict'
const Koa = require('koa')
const app = new Koa()
const config = require('./modules/config.js')
const PORT = process.env.PORT || config.server.port
const Router = require('koa-router')
const router = new Router()
const view = require('koa-nunjucks-next')
const Posts = require('./modules/posts.js')
const posts = Posts.getFolder(config.posts.folder)
app.use(
view(`${__dirname}/views`, {
extname: 'njk'
})
)
const postsArray = Array.from(posts.entries())
router.get('/', async function (ctx) {
await ctx.render('index', {
posts: postsArray
})
})
router.get('/post/:filename', async function (ctx) {
ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found')
const post = posts.get(ctx.params.filename)
post.body = Posts.render(post)
await ctx.render('post', {
post: post
})
})
const taxonomies = Posts.taxonomise(config.taxonomies, posts)
for (let [term, items] of taxonomies) {
router.get(`/${term}/:value`, async function (ctx) {
const value = ctx.params.value
ctx.assert(
items.has(ctx.params.value),
404,
`Could not find ${term} ${value}`
)
await ctx.render('term', {
term: term,
[term]: value,
posts: items.get(ctx.params.value)
})
})
}
app.use(router.routes()).use(router.allowedMethods())
module.exports = app
if (require.main === module) {
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`)
})
}
|