-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
89 lines (61 loc) · 1.86 KB
/
main.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
const SPACE = '';
const ACCESSTOKEN = '';
// main.js
const express = require('express')
const app = express();
const port = 3000;
var exphbs = require('express-handlebars');
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
const contentful = require('contentful');
const documentToHtmlString = require('@contentful/rich-text-html-renderer').documentToHtmlString;
const _ = require('lodash');
var client = contentful.createClient({
space: SPACE,
accessToken: ACCESSTOKEN
});
let getItems = async function () {
let results = await client.getEntries({
content_type: 'progress'
})
.then((response) => { return response.items })
.catch(console.error)
return results;
}
let getItem = async function (slug) {
let results = await client.getEntries({
content_type: 'progress',
'fields.slug': slug
})
.then((response) => { return response.items })
.catch(console.error)
return results;
}
app.get('/tutorial/:slug', async (req, res) => {
let slug = req.params.slug;
let results = await getItem(slug);
if (results[0]) {
steps = _.map(results[0].fields.steps, (step) => {
step.html = documentToHtmlString(step.fields.source);
return step;
});
sidebar = _.map(results[0].fields.steps, (step) => {
return step.fields;
});
}
return res.render('tutorial', {
title: results[0].fields.title,
sidebar,
steps
});
});
app.get('/', async (req, res) => {
let result = await getItems();
console.log(result[0])
return res.render('home', {
tutorials: result
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})