-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
106 lines (88 loc) · 2.88 KB
/
server.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
/* Todo
- icons for delivery and drunk reviews
- handle null values (i.e. if no sauces/drinks ordered. can comment on available selection
)*/
const express = require('express');
const logger = require('morgan');
const dotenv = require('dotenv');
const bodyParser = require('body-parser')
const expressHandlebars = require('express-handlebars');
dotenv.config();
const { sendToHasura } = require('./api/sendToHasura');
const { fetchAllFromHasura } = require('./api/fetchAllFromHasura');
const { fetchReviewsFromHasura } = require('./api/fetchReviewsFromHasura');
const { fetchReviewFromHasura } = require('./api/fetchReviewFromHasura');
// Temporary code to secure live code
const lockEverythingDown = Boolean(process.env.LOCK_EVERYTHING_DOWN === 'true');
const app = express();
app.use(bodyParser.json());
app.engine('hbs', expressHandlebars({
defaultLayout: 'main',
extname: '.hbs'
}));
app.set('view engine', 'hbs');
app.use(logger('dev'));
app.use(express.static(__dirname + '/static'));
app.get('/', async (req, res) => {
const data = await fetchReviewsFromHasura()
.catch(console.error);
res.render('home', {
title: "chickeneaters.",
subtitle: "A guide for eaters of chicken.",
mainImage: "/images/chicken1.jpg",
reviews:data,
});
});
app.get('/review/:id', async (req, res, next) => {
const review = await fetchReviewFromHasura(req.params.id);
if (review) {
res.render("review", {
title: review[0].title,
review: review[0],
mainImage:"/images/reviews1.jpg"
});
}
else{
next();
}
});
app.get('/about', (req, res) => {
res.render('about', {
title: "about chickeneaters.",
mainImage:"/images/chicken2.jpg"
});
});
if (lockEverythingDown !== true) {
app.get('/admin/addReview', async (req, res) => {
const host = process.env.NODE_ENV === 'production' ? 'https://chickeneaters.co.uk' : 'http://localhost:3000';
const chickenShopList = await fetchAllFromHasura(req.body).catch(console.error);
res.render('admin/addReview', {
review: {
chickenPieceRating: 0,
chickenWingRating: 0,
fryRating: 0,
sauceRating: 0,
drinkRating: 0,
overallRating: 0
},
chickenShopList,
});
});
app.post('/api/sendToHasura' , async (req, res) => {
const data = await sendToHasura(req.body)
.catch(console.error);
res.json({
data
})
})
app.get('/api/fetchAllFromHasura' , async (req, res) => {
const data = await fetchAllFromHasura(req.body)
.catch(console.error);
res.json({
data
})
})
}
app.listen(process.env.PORT || 3000, () => {
console.log('Listening on http://localhost:' + (process.env.PORT || 3000));
});