-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (70 loc) · 2.06 KB
/
Copy pathapp.js
File metadata and controls
81 lines (70 loc) · 2.06 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
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");
app.set("view engine","ejs");
app.set("views",path.join(__dirname,"views"));
app.use(express.urlencoded({extended:true}));
app.use(methodOverride("_method"))
app.engine("ejs",ejsMate);
app.use(express.static(path.join(__dirname, "/public")))
async function main() {
await mongoose.connect('mongodb://localhost:27017/wanderlust');
}
main().then(()=>{
console.log("connected to db")
})
.catch((err)=>{
console.log(err);
});
app.get("/", (req,res)=>{
res.send("hii this is working");
});
//new route
app.get("/listings/new",(req,res)=>{
res.render("listings/new.ejs")
})
//show route
app.get("/listings/:id", async (req,res)=>{
let {id}= req.params;
const listing = await Listing.findById(id);
res.render("listings/show.ejs", {listing});
})
//Index Route
app.get("/listings", async (req,res)=>{
const allListings = await Listing.find({});
res.render("./listings/index.ejs",{allListings});
})
//Create Route
app.post("/listings", async (req,res)=>{
// let {title, description,image , price,country,location} = res.params;
//let listing = req.body.listing;
const newListing = new Listing(req.body.listing);
await newListing.save();
res.redirect("/listings");
})
// edit route
app.get("/listings/:id/edit",async (req,res)=>{
let {id} = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit.ejs", {listing});
});
//update route
app.put("/listings/:id",async (req,res)=>{
let { id }= req.params;
await Listing.findByIdAndUpdate(id,{...req.body.listing});
res.redirect("/listings")
});
//Delete route
app.delete("/listings/:id", async (req,res)=>{
let { id }= req.params;
let deletedListing = await Listing.findByIdAndDelete(id);
console.log(deletedListing);
res.redirect("/listings");
})
app.listen(8080,()=>{
console.log("server is listening on port 8080")
});