const express = require("express");
const app = express();
const path = require("path");
let port = 8080;
const { v4: uuidv4 } = require('uuid');
app.use(express.urlencoded({extended: true}));
app.set("views engine","ejs");
app.set("views",path.join(__dirname,"views"));
app.use(express.static(path.join(__dirname,"public")));
let posts =[{ id: uuidv4(),username : "Naved",content : "I love coding",},{id: uuidv4(),username :"Noman",content : "I love studying",},{id: uuidv4(),username : "Saimoon",content:"Ilovecooking",}] ;
app.get("/posts",(req,res)=>{
res.render("index.ejs", {posts});
});
app.get("/posts/new",(req,res)=>{
res.render("new.ejs")
})
app.post("/posts",(req,res)=>{
let {username,content} = req.body;
let id = uuidv4();
posts.push({id,username , content});
res.redirect("/posts");
})
app.get("/posts/:id",(req,res)=>{
let { id } = req.params;
let post = posts.find ((p)=> id===p.id);
res.render("show.ejs" ,{post})
})
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;
console.log(post);
res.send("patch request send succesfully");
})
app.listen(port,()=>{
console.log(`app is listening at the port ${port}`);
});
script.js
The code seems to be working fine. However, there might be an issue with how you are handling req.body.content. Make sure to check for any typos or missing information in your request body.