-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (122 loc) · 3.45 KB
/
index.js
File metadata and controls
146 lines (122 loc) · 3.45 KB
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
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const fs = require('fs');
const Fruit = require('./models/fruit');
const Order = require('./models/order');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: true })); // added during week 6 assignment
app.use(express.json()); // added during week 6 assignment
const PORT = process.env.PORT || 3000;
const conn = 'mongodb+srv://web340_user:admin@cluster0.lujih.mongodb.net/web340DB?retryWrites=true&w=majority'; // added in week 6
// added in week 6
mongoose.connect(conn).then(() => {
console.log('Connection to the database was successful');
}).catch(err => {
console.log('MongoDB Error: ' + err.message);
})
app.get('/', (req, res) => {
res.render('index', {
title: 'FMS: Landing'
})
});
app.get('/about', (req, res) => {
res.render('about', {
title: 'FMS: About',
pageTitle: 'Our Story!'
})
})
app.get('/contact', (req, res) => {
res.render('contact', {
title: 'FMS: Contact',
pageTitle: 'Our Locations!'
})
})
app.get('/fruit-create', (req, res) => {
res.render('fruit-create', {
title: 'FMS: Fruit Form',
pageTitle: 'Create Fruit'
})
})
app.post('/fruits', (req, res, next) => {
console.log(req.body);
console.log(req.body.fruitName);
const newFruit = new Fruit({
name: req.body.fruitName
})
console.log(newFruit);
Fruit.create(newFruit, function(err, fruit) {
if (err) {
console.log(err);
next(err);
} else {
res.render('index', {
title: 'FMS: Landing'
})
}
})
})
app.get('/fruits', (req, res) => {
Fruit.find({}, function(err, fruits) {
if (err) {
console.log(err);
next(err);
} else {
res.render('fruit-list', {
title: 'FMS: Fruit List',
pageTitle: 'Our Inventory',
fruits: fruits
})
}
})
})
app.get('/order', (req, res) => {
let jsonFile = fs.readFileSync('./public/data/payments.json');
let payments = JSON.parse(jsonFile);
console.log(payments);
res.render('order', {
title: 'Pets-R-Us: Customer Order Form',
pageTitle: 'Order Now!',
payments: payments
})
})
app.post('/order', (req, res, next) => {
const newOrder = new Order({
firstName: req.body.firstName,
lastName: req.body.lastName,
payment: req.body.payment,
total: req.body.total
})
Order.create(newOrder, function(err, order) {
if (err) {
console.log(err);
next(err);
} else {
res.render('index', {
title: 'FMS: Landing Page'
})
}
})
})
app.get('/my-orders', (req, res) => {
res.render('my-orders', {
title: 'FMS: My Orders',
pageTitle: 'Order Lookup Form'
})
})
app.get('/api/orders/:lastName', async(req, res, next) => {
Order.find({'lastName': req.params.lastName}, function(err, orders) {
if (err) {
console.log(err);
next(err);
} else {
res.json(orders);
}
})
})
app.listen(PORT, () => {
console.log('Application started and listening on PORT ' + PORT);
});