-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (114 loc) · 3.17 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
138
139
140
141
142
143
144
145
146
require("dotenv").config()
const logger = require("morgan")
const express = require("express")
const errorHandler = require("errorhandler")
const bodyParser = require("body-parser")
const methodOverride = require("method-override")
const app = express()
const path = require("path")
const port = 3000
app.use(logger("dev"))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(methodOverride())
app.use(errorHandler())
app.use(express.static(path.join(__dirname, "public")))
const Prismic = require("@prismicio/client")
const PrismicDOM = require("prismic-dom")
const initApi = req => {
return Prismic.getApi(process.env.PRISMIC_ENDPOINT, {
accessToken: process.env.PRISMIC_ACCESS_TOKEN,
req
})
}
const handleLinkResolver = (doc) => {
if(doc.type === "product")
{
return `/detail/${doc.uid}`
}
if(doc.type === "collections")
{
return "/collections"
}
if(doc.type === "about")
{
return "/about"
}
return '/'
}
app.use((req, res, next) => {
res.locals.ctx = {
endpoint: process.env.PRISMIC_ENDPOINT,
linkResolver: handleLinkResolver
}
res.locals.PrismicDOM = PrismicDOM
res.locals.Number = (index) => {
return index == 0 ? "One" : index == 1 ? "Two" : index == 2 ? "Three" : index == 3 ? "Four" : ""
}
res.locals.Link = handleLinkResolver
next()
})
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "pug")
const handleRequest = async(api) => {
const metadata = await api.getSingle("metadata")
const preloader = await api.getSingle("preloader")
const navigation = await api.getSingle("navigation")
return {
metadata,
preloader,
navigation
}
}
app.get('/', async (req, res) => {
const api = await initApi(req)
const home = await api.getSingle("home")
const defaults = await handleRequest(api)
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("/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"
})
collections.forEach(element => {
console.log(element.data.products[0].products_product.data)
});
res.render("pages/collections", {
...defaults,
home,
collections,
})
})
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.listen(port, () => {
console.log(`Example app listening on http://localhost:${port}`)
})