-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (119 loc) · 4.73 KB
/
index.js
File metadata and controls
133 lines (119 loc) · 4.73 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require("express");
const app = express();
const port = 5173;
const path = require("path");
const { v4: uuidv4 } = require("uuid"); //
const methodOverride = require("method-override");
app.use(methodOverride('_method'));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
// app.use(express.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.listen(port, () =>{
console.log(`Listening on port : ${port}`);
})
// login page
app.get("/", (req, res)=>{
// res.json("Building a new Project!!");
res.render("login.ejs");
})
let posts = [
{
id: uuidv4(),
username: "aryangupta",
img: "https://images.unsplash.com/photo-1761839258657-457dda39b5cc?ixlib=rb-4.1.0&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=2670",
caption: "I can show you the world.",
likes: 980,
comments: 140,
},
{
id: uuidv4(),
username: "abhaygupta",
img: "https://plus.unsplash.com/premium_photo-1761295133736-8c68192dd522?ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=2670",
caption: "Be happy, it drives people crazy.",
likes: 900,
comments: 102,
},
{
id: uuidv4(),
username: "safalchaubey",
img: "https://images.unsplash.com/photo-1761839257961-4dce65b72d99?ixlib=rb-4.1.0&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=3264",
caption: "But first, let me take a selfie.",
likes: 89,
comments: 112,
},
{
id: uuidv4(),
username: "avnigupta",
img: "https://images.unsplash.com/photo-1513682121497-80211f36a7d3?ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=1288",
caption: "Escape the ordinary.",
likes: 90,
comments: 112,
},
{
id: uuidv4(),
username: "symuncorpo",
img: "https://images.unsplash.com/photo-1735753817845-a85f65b94df7?ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=1287",
caption: " Life is too short for bad vibes.",
likes: 90,
comments: 112,
},
{
id: uuidv4(),
username: "icrowreus",
img: "https://images.unsplash.com/photo-1762319981432-609103ab4a75?ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&q=80&w=2675",
caption: "Sunkissed and blessed.",
likes: 90,
comments: 112,
}
];
app.get("/posts", (req, res) => {
// console.log(req.query);
res.render("index.ejs", {posts});
});
app.get("/posts/new", (req, res)=>{
// res.send("creating a new post");
res.render("new.ejs"); // get request to display our form page
});
app.post("/posts", (req, res) => {
let{username, img, caption, likes, comments} = req.body; // recall in post req extrace data from req.body using deconstruc method
posts.push({id: uuidv4(), username, img, caption, likes, comments}); // for emage enter link of image from unsplash
console.log(posts);
res.redirect("/posts");
})
app.get("/posts/:id", (req,res)=>{ // get request on post-id to get view of that post only
let {id} = req.params;
// console.log("id: " + id);
let post = posts.find((p) => id === p.id);
// console.log(post);
res.render("show.ejs",{post}); // this is wrong becoz i am sending whole of the posts array
// instead identify post with the help of id first then send that post only
})
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.post("/posts/:id/edit", (req,res) => {
const {id} = req.params;
// let {newCaption} = req.body.caption;
let newCaption = req.body.caption; // note imp when we have strings don't do deconstruction
let post = posts.find((p) => id === p.id);
// console.log(id);
post.caption = newCaption;
res.redirect("/posts");
});
// note when pushings object in post in object each property is a key value pair like username is username: "aryan"
// so to send uuidv4() make it also key value pair id: uuidv4();
// then view post ka option
// -> show post -> get req on posts/id: -> renders -> show.ejs
// now post ko edit option dedo jisme caption edit ho sake
// delete post ka option hai ik
app.delete("/posts/:id", (req,res) => {
let {id} = req.params;
posts = posts.filter((p)=> p.id !== id);
// res.send("Deleting post");
// console.log(posts);
res.redirect("/posts");
})