-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (74 loc) · 2.03 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
// universal code -------------------------
const express = require("express");
const app = express();
const port = 8080;
// uuid
const { v4: uuidv4 } = require("uuid");
// mehtod-override
const methodOverride = require("method-override");
app.use(methodOverride("_method"));
const path = require("path");
const { log } = require("console");
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.listen(port, () => {
console.log(`Listen to port : ${port}`);
});
//-----------------------------------------
// raw data
let posts = [
{
id: uuidv4(),
username: "pravin",
content: "I love coding",
},
{
id: uuidv4(),
username: "ganesh",
content: "I am hardworking",
},
{
id: uuidv4(),
username: "amol",
content: "I got selected for my 1st internship",
},
];
app.get("/posts", (req, res) => {
res.render("index.ejs", { posts });
});
//---- post
app.get("/posts/new", (req, res) => {
res.render("new.ejs");
});
app.post("/posts", (req, res) => {
let { username, content } = req.body; // destructur and find username and content
let id = uuidv4();
posts.push({ id, username, content });
res.redirect("/posts"); // after clicking submit button go to home or posts page
});
app.get("/posts/:id", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => id === p.id);
res.render("show.ejs", { post });
});
// ------- Patch = update
app.get("/posts/:id/edit", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => id === p.id);
res.render("edit.ejs", { post });
});
app.patch("/posts/:id", (req, res) => {
let { id } = req.params; //get
let newContent = req.body.content; //post
let post = posts.find((p) => id === p.id);
post.content = newContent;
res.redirect("/posts");
});
// ---------Delet
app.delete("/posts/:id", (req, res) => {
let { id } = req.params;
posts = posts.filter((p)=>id!==p.id);
res.redirect("/posts");
});