-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (65 loc) · 2.43 KB
/
app.js
File metadata and controls
93 lines (65 loc) · 2.43 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
const express = require("express");
const mongoose = require("mongoose");
const Listing = require("./models/listing");
const path = require("path");
const methodOvr = require("method-override");
const ejsMate = require("ejs-mate");
const app = express();
mongoose.connect("mongodb://127.0.0.1:27017/wanderlust")
.then( () => console.log("Connected to MongoDB"))
.catch(err => console.log("Failed to connect to MongoDB", err));
app.set("view engine", "ejs");
app.set("views",path.join(__dirname, "views") );
app.engine("ejs", ejsMate);
app.use(express.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(methodOvr("_method"));
app.get("/", (req, res) => {
res.send("Hello i am root");
});
app.get("/listings", (req, res) => {
Listing.find({})
.then(result => res.render("listing", {listings: result}))
.catch(err => console.log("Error in fetching all listings: ", err));
});
app.get("/listings/edit/:id", (req, res) => {
const id = req.params.id;
Listing.findById(id)
.then(list => res.render("edit.ejs", {list}))
.catch(err => console.log("Error in finding listing in database"));
});
app.get("/listings/new", (req, res) => {
res.render("new.ejs");
});
app.get("/listings/:id", (req, res) => {
let {id} = req.params;
Listing.findById(id)
.then(result => res.render("listingDetails.ejs", {list: result}))
.catch(err => console.log("Error in fetching listing details: ", err));
});
app.post("/listings", (req, res) => {
const listing = new Listing(req.body.listing);
listing
.save()
.then(result => res.redirect("/listings"))
.catch(err => console.log("Error in creating new listing: ", err));
});
app.put("/listings/:id", (req, res) => {
const id = req.params.id;
Listing.findByIdAndUpdate(id, req.body.listing, {new: true})
.then(result => {
console.log("Updated listing: ", result);
res.redirect("/listings");
})
.catch(err => console.log("Error in updating listing: ", err));
});
app.delete("/listings/:id", (req, res) => {
const {id} = req.params;
Listing.findByIdAndDelete(id)
.then(result => res.redirect("/listings"))
.catch(err => console.log("Error in deleting listing: ", err));
});
app.listen(8080, () => {
console.log("Servr is running on port 8080");
});