-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (83 loc) · 2.33 KB
/
index.js
File metadata and controls
96 lines (83 loc) · 2.33 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
const express = require('express');
const app = express();
const path = require('path');
const methodOverride = require('method-override')
const { v4: uuid } = require('uuid')
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(methodOverride('_method'))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
let comments = [
{
id: uuid(),
username: 'Todd',
comment: 'lol that is so funny'
},
{
id: uuid(),
username: 'lolzor',
comment: 'Mighty harvest is a lame subreddit'
},
{
id: uuid(),
username: 'Sk8terBoi',
comment: 'Yo'
},
{
id: uuid(),
username: 'She',
comment: 'see ya later, boi'
},
]
// Begin CRUD Implementation
// Index for all comments
app.get('/comments', (req, res) => {
res.render('comments/index', { comments })
})
// Serves a form for adding comments
app.get('/comments/new', (req, res) => {
res.render('comments/new')
})
// Appends comments with a new one.
app.post('/comments', (req, res) => {
const { username, comment } = req.body;
comments.push({ username, comment, id: uuid() })
res.redirect('/comments')
})
// SHOW a particular comment
app.get('/comments/:id', (req, res) => {
const { id } = req.params;
const comment = comments.find(c => c.id === id)
res.render('comments/show', { comment })
})
// Update a comment
app.get('/comments/:id/edit', (req, res) => {
const { id } = req.params;
const comment = comments.find(c => c.id === id);
res.render('comments/edit', { comment })
})
app.patch('/comments/:id', (req, res) => {
const { id } = req.params;
const newCommentText = req.body.comment;
const foundComment = comments.find(c => c.id === id);
foundComment.comment = newCommentText;
res.redirect('/comments')
})
// Destroy a comment. BEEP BOOP!
app.delete('/comments/:id', (req, res) => {
const { id } = req.params;
comments = comments.filter(c => c.id !== id);
res.redirect('/comments')
})
// END CRUD Implementation
app.get('/tacos', (req, res) => {
res.send('GET /tacos response')
})
app.post('/tacos', (req, res) => {
const { meat, qty } = req.body;
res.send(`OK. Here are your ${qty} ${meat} taco(s)`)
})
app.listen(3000, () => {
console.log('On port 3000!')
})