-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (86 loc) · 2.74 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
import Express from 'express';
import exphbs from 'express-handlebars';
import path from 'path';
import { getRecipeById, getRecipesByCategory, getRecipesBySearch } from './API_tools.js';
import { ingredientsFormatter, sendEmail, recipeEmailFormatter } from './tools.js';
// Middleware
const app = Express();
const __dirname = path.resolve();
app.engine(
'handlebars',
exphbs({
defaultLayout: 'main',
})
);
app.set('view engine', 'handlebars');
app.use(Express.static(__dirname));
app.use(
Express.urlencoded({
extended: true,
})
);
// Handlers
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
app.get('/categories', (req, res) => {
res.render('categories');
});
app.get('/search', async (req, res) => {
const categories = [
'SEAFOOD',
'BEEF',
'PORK',
'CHICKEN',
'PASTA',
'BREAKFAST',
'VEGETARIAN',
'LAMB',
'DESSERT',
];
const searchQuery = req.query.query;
let meals = [];
if (categories.indexOf(searchQuery.toUpperCase()) == -1) {
meals = await getRecipesBySearch(searchQuery);
} else {
meals = await getRecipesByCategory(searchQuery);
}
let errors;
meals === null ? (errors = true) : (errors = false);
res.render('recipe-list', {
searchQuery,
meals,
errors,
});
});
app.get('/meal', async (req, res) => {
let meal = await getRecipeById(req.query.id);
meal.strInstructions = meal.strInstructions.split('\n');
meal = ingredientsFormatter(meal);
meal.ingredientsString = meal.ingredients.join();
res.render('recipe', {
meal,
});
});
app.get('/archives', (req, res) => {
res.render('archives');
});
app.get('/contact', (req, res) => {
res.render('contact');
});
app.post('/sendSupportEmail', async (req, res) => {
const { name, email, message } = req.body;
const newMessage = `Name: ${name}\nEmail: ${email}\n\n${message}`;
sendEmail('[email protected]', 'Support Ticket!', newMessage);
const displayMessage = 'Thank you for your email! I will reply as soon as possible.';
res.render('messages', { displayMessage });
});
app.post('/sendIngredientsEmail', async (req, res) => {
const ingredients = req.body.hiddenIngredients;
const recipeNames = req.body.hiddenRecipeNames;
const message = recipeEmailFormatter(recipeNames, ingredients);
const { email } = req.body;
sendEmail(email, 'Ingredients List!', message);
const displayMessage = 'Your email will arive shortly! \n Be sure to check the spam folder';
res.render('messages', { displayMessage });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port: ${PORT}`));