CREATE ROUTE(NEW)

 INDEX.JS


// NEW ROUTE
app.get("/listings/new", (req, res) => {
  res.render("listings/new.ejs");
});
app.post("/listings", async (req, res) => {

  const newListing=new Listing(req.body.listing);
  await newListing.save();
  res.redirect("/listings");
});


***TO PARSE****

app.use(express.urlencoded({ extended: true }));

app.use(express.urlencoded({extended:true}))

NEW.EJS

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h2>Add new listing</h2>

    <form method="POST" action="/listings">
      <input name="listing[title]" type="text" placeholder="Enter title" />
      <textarea
        name="listing[description]"
        type="text"
        placeholder="write description"
      ></textarea>
      <input
        name="listing[image]"
        type="text"
        placeholder="paste image URL/link"
      />
      <input
        name="listing[location]"
        type="text"
        placeholder="enter location"
      />
      <input name="listing[country]" type="text" placeholder="enter country" />
      <button>ADD</button>
    </form>
  </body>
</html>

INDEX.EJS

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WanderLust</title>
  </head>
  <body>
    <h2>All Lists</h2>
    <form method="get" action="/listings/new">
      <button>Add new Listing</button>
    </form>
    <% for(listing of AllListings){%>
      <ul>
        <a href="/listings/<%=listing._id%>"><%= listing.title%></a>
      </ul>
    <%}%>
  </body>
</html>




Comments