-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (72 loc) · 2.18 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
const express = require("express")
const path = require("path")
const {v4:uuidv4} = require("uuid")
const methodOverride = require("method-override")
const { send } = require("process")
const app = express()
const PORT = 8000
app.use(express.urlencoded({extended: true}))
app.use(methodOverride("_method"))
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.use(express.static(path.join(__dirname, 'public')))
let posts = [
{
id : uuidv4(),
username : "Virang",
content : "DNA is like a computer program but far, far more advanced than any software ever created."
},
{
id : uuidv4(),
username : "Abhinav",
content : "There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors."
},
{
id : uuidv4(),
username : "Uday",
content : "I would tell you a UDP joke, but you might not get it."
},
{
id : uuidv4(),
username : "Meet",
content : "There are 10 types of people in the world: those who understand binary, and those who don’t."
}
]
app.get("/", (req, res) => {
res.redirect("/posts")
})
app.get("/posts", (req,res) => {
res.render("index.ejs", {posts})
})
app.get("/posts/new", (req, res) => {
res.render("new");
})
app.get("/posts/:id", (req, res) => {
let { id } = req.params
let post = posts.find((p) => id === p.id)
res.render("show", {post})
})
app.post("/posts", (req,res)=> {
let {username, content} = req.body
let id = uuidv4()
posts.push({id, username, content})
res.redirect("/posts")
})
app.patch("/posts/:id", (req, res) => {
let {id} = req.params
let newContent = req.body.content
let post = posts.find((p) => id === p.id)
post.content = newContent
res.redirect("/posts")
})
app.delete("/post/:id", (req, res) => {
let {id} = req.params
posts = posts.filter((p) => id != p.id)
res.redirect("/posts")
})
app.get("/posts/:id/edit", (req, res) => {
let {id} = req.params
let post = posts.find((p) => id === p.id)
res.render("edit", {post})
})
app.listen(PORT, () => console.log(`Server started at ${PORT}`))