0% found this document useful (0 votes)
8 views

code in

Uploaded by

hspandit071
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

code in

Uploaded by

hspandit071
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

### `index.

ejs`
This page displays all the listings:
```ejs
<% layout("/layouts/boilerplate") %>

<h3>All Listings</h3>

<div class="row row-cols-lg-3 row-cols-md-2 row-cols-sm-1">


<% for(let listing of allListings) { %>
<a href="/listings/<%= listing._id %>" class="listing-link">
<div class="card col">
<img src="<%= listing.image.url %>"
class="card-img-top"
alt="listing_image"
style="height:20rem">
<div class="card-img-overlay">abch</div>
<div class="card-body">
<p class="card-text">
<b><%= listing.title %></b><br>
&#8377; <%= listing.price.toLocaleString("en-IN") %> /night
</p>
</div>
</div>
</a>
<% } %>
</div>
```

### `show.ejs`
This page shows the details of a single listing:
```ejs
<% layout("/layouts/boilerplate") %>

<h1><%= listing.title %></h1>


<img src="<%= listing.image.url %>" alt="Listing Image" style="max-width: 100%;
height: auto;">
<p><strong>Description:</strong> <%= listing.description %></p>
<p><strong>Price:</strong> &#8377; <%= listing.price.toLocaleString("en-IN") %>
/night</p>
<p><strong>Location:</strong> <%= listing.location %>, <%= listing.country %></p>
<a href="/listings" class="btn btn-primary">Back to Listings</a>
```

### `app.js`
Here’s your server-side code:
```javascript
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Listing = require("./models/listing.js");
const path = require("path");
const methodOverride = require("method-override");
const ejsMate = require("ejs-mate");

const MONGO_URL = "mongodb://localhost:27017/wanderlust";

main()
.then(() => {
console.log("Connected to MongoDB");
})
.catch((err) => {
console.log("Connection Error:", err);
});

async function main() {


await mongoose.connect(MONGO_URL);
}

app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));

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


app.use(methodOverride("_method"));
app.use(express.static(path.join(__dirname, "/public")));

app.get("/", (req, res) => {


res.send("Hi, I am root");
});

// Index Route
app.get("/listings", async (req, res) => {
const allListings = await Listing.find({});
res.render("listings/index.ejs", { allListings });
});

// New Route
app.get("/listings/new", (req, res) => {
res.render("listings/new.ejs");
});

// Show Route
app.get("/listings/:id", async (req, res) => {
const { id } = req.params;
const listing = await Listing.findById(id);
if (!listing) {
return res.status(404).send("Listing not found");
}
res.render("listings/show.ejs", { listing });
});

// Create Route
app.post("/listings", async (req, res) => {
const newListing = new Listing(req.body.listing);
await newListing.save();
res.redirect("/listings");
});

// Edit Route
app.get("/listings/:id/edit", async (req, res) => {
const { id } = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit.ejs", { listing });
});

// Update Route
app.put("/listings/:id", async (req, res) => {
const { id } = req.params;
await Listing.findByIdAndUpdate(id, { ...req.body.listing });
res.redirect(`/listings/${id}`);
});

// Delete Route
app.delete("/listings/:id", async (req, res) => {
const { id } = req.params;
await Listing.findByIdAndDelete(id);
res.redirect("/listings");
});

app.listen(8080, () => {
console.log("Server is listening on port 8080");
});
```

### `models/listing.js`
Here’s the schema for the listings:
```javascript
const mongoose = require("mongoose");

const listingSchema = new mongoose.Schema({


title: String,
description: String,
price: Number,
location: String,
country: String,
image: {
url: String,
filename: String,
},
});

module.exports = mongoose.model("Listing", listingSchema);


```

You might also like