-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (78 loc) · 2.52 KB
/
index.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
// import express from 'express';
// import monsterData from './monsters.json' assert { type: 'json' };
import * as dotenv from 'dotenv';
import path from 'path'
import express from 'express'
import { fileURLToPath } from 'url'
import * as prismicH from '@prismicio/helpers'
import { client as prismicClient } from './config/prismicConfig.js'
// import contentful from 'contentful';
// const client = contentful.createClient({
// space: '8gjcuad3dr24',
// environment: 'master', // defaults to 'master' if not set
// accessToken: 'qo2CFiuKeIkYyUD6lUM0302dAeo2EoG_MayfVfOCySo'
// })
dotenv.config()
const port = process.env.PORT || 1982;
const app = express();
app.set('view engine', 'ejs');
// Public asset folders
app.use( express.static('public') );
app.use( express.static('css') );
app.use((req, res, next) => {
res.locals.ctx = {
prismicH,
}
next()
})
// Page routes
app.get('/', function(request, response) {
// client.getEntry('3jblgGPeOqxPhG4vVv6XbR')
// .then( function(entry) {
// response.render('home', { page: entry.fields });
// })
// .catch(console.error)
// ;
});
app.get('/products', async function(request, response) {
const products = await prismicClient.getAllByType('product');
response.render('products', { products });
});
app.get('/products/:uid', async function(request, response) {
const specificProduct = await prismicClient.getByUID('product', request.params.uid);
response.render('product', { specificProduct });
});
app.get('/monsters', function(request, response) {
// client.getEntries({
// content_type: 'monster'
// })
// .then( function(data) {
// const monsterData = data.items.map( function(item) {
// return {
// name: item.fields.name,
// story: item.fields.story.content.content[0].value,
// portrait: item.fields.portrait.fields.file.url,
// adopted: item.fields.adopted,
// birthday: item.fields.birthday,
// color: item.fields.color,
// };
// });
// console.log(monsterData);
// response.render('monsters', { monsters: monsterData });
// })
// .catch(console.error);
});
app.get('/monsters/:slug', function(request, response) {
const monster = monsterData.find( function(monster) {
return monster.slug == request.params.slug;
});
response.render('detail', { monster });
});
// If no route matches...
app.use( function(request, response) {
response.status(404).render('404', { query: request.url });
});
// Start app
app.listen(port, function() {
console.log("Server started at http://localhost:1982");
});