-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (84 loc) · 2.5 KB
/
index.js
File metadata and controls
99 lines (84 loc) · 2.5 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
var c = (x) => console.log(x);
const { request } = require('express');
const express = require('express');
var methodOverride = require('method-override')
const app = express();
const path = require('path');
const { v4: uuidv } = require('uuid');
// Listen to port carefully and decide the port no
app.listen(3000, () => {
c("Listening on port 3000");
})
app.use(methodOverride('_method'))
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Initial Local Database
let comments = [
{
id: uuidv(),
username: 'Tomorrow Meeting',
comment: 'Have to meet one more A series client'
},
{
id: uuidv(),
username: 'Client Feedback',
comment: 'Need to create a feedback report of the client'
},
{
id: uuidv(),
username: 'Movie',
comment: 'Need to plan a new movie with friends'
}
];
app.get('/comments', (req, res) => {
res.render('comments/index', { comments });
})
app.get('/comments/new', (req, res) => {
res.render('comments/new');
})
app.get('/comments/submitted', (req, res) => {
res.render('./comments/submitted');
})
app.get('/comments/:id', (req, res) => {
const { id } = req.params;
const comment = comments.find(c => c.id === id);
res.render('comments/show', { id, comment });
})
app.get('/comments/:id/edit', (req, res) => {
const { id } = req.params;
const comment = comments.find(c => c.id === id);
res.render('comments/edit', { id, comment });
})
app.post('/comments', (req, res) => {
const { username, comment } = req.body;
// c(bodi) ;
let idx = comments.length - 1;
let newId = comments[idx].id + 1;
comments.push({ id: newId, username, comment });
c(comments);
res.redirect('comments/submitted');
})
app.patch('/comments/:id', (req, res) => {
const {id} = req.params ;
const newComment = req.body.comment ;
// c(newComment) ;
let oldComment = comments.find(c => c.id === id) ;
oldComment.comment = newComment ;
res.redirect('/comments/')
})
app.delete('/comments/:id', (req, res) => {
const {id} = req.params ;
comments = comments.filter((c) => {
return c.id !== id ;
})
c(comments) ;
res.redirect('/comments/')
})
app.get('/' , (req, res) => {
res.redirect('/comments') ;
})
app.get('/:anything' , (req, res) => {
res.send('Kya kar r hai bhai tu!') ;
})