-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (84 loc) · 2.58 KB
/
index.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
107
const path = require('path');
const methodOverride = require('method-override')
const { v4: uuid } = require('uuid'); //For generating ID's
const express = require('express');
const app = express();
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: 'Skyler',
comment: 'I like to go birdwatching with my dog'
},
{
id: uuid(),
username: 'Sk8erBoi',
comment: 'Plz delete your account, Todd'
},
{
id: uuid(),
username: 'onlysayswoof',
comment: 'woof woof woof'
}
]
app.get('/comments', (req, res) => {
res.render('comments/index', { comments });
})
app.get('/comments/new', (req, res) => {
res.render('comments/new');
})
app.post('/comments', (req, res) => {
const { username, comment } = req.body;
comments.push({ username, comment, id: uuid() })
res.redirect('/comments');
})
app.get('/comments/:id', (req, res) => {
const { id } = req.params;
const comment = comments.find(c => c.id === id);
res.render('comments/show', { 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 foundComment = comments.find(c => c.id === id);
//get new text from req.body
const newCommentText = req.body.comment;
//update the comment with the data from req.body:
foundComment.comment = newCommentText;
//redirect back to index (or wherever you want)
res.redirect('/comments')
})
// DELETE/DESTROY- removes a single comment
app.delete('/comments/:id', (req, res) => {
const { id } = req.params;
comments = comments.filter(c => c.id !== id);
res.redirect('/comments');
})
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} tacos`)
})
app.listen(3000, () => {
console.log("ON PORT 3000!")
})
// GET /comments - list all comments
// POST /comments - Create a new comment
// GET /comments/:id - Get one comment (using ID)
// PATCH /comments/:id - Update one comment
// DELETE /comments/:id - Destroy one comment