-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (114 loc) · 3.38 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
133
134
135
136
137
require('dotenv').config()
// console.log(process.env.PRISMIC_ENDPOINT, process.env.PRISMIC_CLIENT_ID)
const express = require('express')
const errorHandler = require('errorhandler')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const logger = require('morgan')
const app = express()
const path = require('path')
const port = 3000
const Prismic = require('@prismicio/client');
const PrismicDOM = require('prismic-dom');
const { defaultMaxListeners } = require('events')
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false}))
app.use(methodOverride())
app.use(errorHandler())
// Link Resolver
const handleLinkResolver = doc => {
// Define the url depending on the document type
if (doc.type === 'product') {
return '/details/' + doc.uid;
} else if (doc.type === 'about') {
return '/about';
} else if (doc.type === 'collections') {
return '/collections';
} else {
// // Default to homepage
return '/';
}
}
// Initialize the prismic.io api
const initApi = req => {
return Prismic.getApi(process.env.PRISMIC_ENDPOINT, {
accessToken: process.env.PRIMSIC_ACCESS_TOKEN,
req: req
});
}
// Middleware to inject prismic context
app.use((req, res, next) => {
// res.locals.ctx = {
// endpoint: process.env.PRISMIC_ENDPOINT,
// linkResolver: handleLinkResolver,
// };
// add PrismicDOM in locals to access them in templates.
res.locals.Link = handleLinkResolver;
res.locals.PrismicDOM = PrismicDOM;
res.locals.Numbers = index => {
return index == 0 ? 'One' : index == 1 ? 'Two' : index == 2 ? 'Three' : index == 3 ? 'Four' : index == 5 ? 'Five' : '';
}
next();
});
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
const handleRequest = async api => {
const meta = await api.getSingle('meta')
const navigation = await api.getSingle('navigation')
const preloader = await api.getSingle('preloader')
return {
meta,
navigation,
preloader
}
}
app.get('/', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
const home = await api.getSingle('home')
const { results: collections } = await api.query(Prismic.Predicates.at('document.type', 'collection'), {
fetchLinks: 'product.image'
})
res.render('pages/home', {
...defaults,
collections,
home
})
})
app.get('/about', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
const about = await api.getSingle('about')
res.render('pages/about', {
...defaults,
about
})
})
app.get('/detail/:uid', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
const product = await api.getByUID('product', req.params.uid, {
fetchLinks: 'collection.title'
})
res.render('pages/detail', {
...defaults,
product
})
})
app.get('/collections', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
const home = await api.getSingle('home')
const { results: collections } = await api.query(Prismic.Predicates.at('document.type', 'collection'), {
fetchLinks: 'product.image'
})
res.render('pages/collections', {
...defaults,
collections,
home
})
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})