-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 2.02 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
const express = require("express");
const app = express();
var methodOverride = require("method-override");
app.use(methodOverride("_method"));
const { v4: uuidv4 } = require("uuid");
const port = 8080;
const path = require("path");
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")));
let posts = [
{
id: uuidv4(),
username: "apna collge",
content: "I love coding",
},
{
id: uuidv4(),
username: "shahwaiz",
content: "I love dancing",
},
{
id: uuidv4(),
username: "islam",
content: "I love singing",
},
];
app.get("/", (req, res) => {
//res.send("server running well");
res.render("index.ejs", { posts });
});
//implement post
app.get("/new", (req, res) => {
res.render("new.ejs");
});
app.post("/", (req, res) => {
let { username, content } = req.body;
// console.log(req.body);
let id = uuidv4();
posts.push({ id, username, content });
res.redirect("/");
res.send("post request working");
});
//show a particular post
app.get("/:id", (req, res) => {
let { id } = req.params;
// console.log(id);
let post = posts.find((p) => id === p.id);
// res.send("request working");
res.render("show.ejs", { post });
});
//to update a specific post
app.patch("/:id", (req, res) => {
let { id } = req.params;
let newContent = req.body.content;
let post = posts.find((p) => id === p.id);
post.content = newContent;
// console.log(newContent);
// console.log(id);
console.log(post);
// res.send("patch request working ");
res.redirect("/");
});
app.get("/:id/edit", (req, res) => {
let { id } = req.params;
let post = posts.find((p) => id === p.id);
res.render("edit.ejs", { post });
});
app.delete("/:id", (req, res) => {
let { id } = req.params;
posts = posts.filter((p) => id !== p.id);
//res.send("delete success");
res.redirect("/");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});