-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (54 loc) · 1.63 KB
/
app.js
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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Listing = require("./models/listing")
const MONGODB_URL = "mongodb://127.0.0.1:27017/airbnb";
const PORT = 7700;
main()
.then(() => {
console.log("connected to DB");
})
.catch((err) => {
console.log(err);
});
async function main() {
await mongoose.connect(MONGODB_URL)
}
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({extended: true }));
app.use(methodOverride("_method"));
app.get("/listings", async (req, res) => {
const allListings = await Listing.find({});
allListings.then((res) =>{
console.log(res);
})
// render react component
});
app.get("/listings/:id", async (req, res) => {
let {ID} = req.params;
const matchListing = await Listing.findById(ID);
// render react component
});
app.post("/listings", async (req, res) => {
const newListingInfo = req.body.listing;
const newListing = new Listing(newListingInfo);
await newListing.save();
});
app.get("/listings/:id/edit", async (req, res) => {
let {ID} = req.params;
const listing = await Listing.findById(ID);
// render react component
});
app.put("/listings/:id/update", async (req, res) => {
let {ID} = req.params;
Listing.findByIdAndUpdate(ID, {...req.body.listing});
// render react component
});
app.delete("/listings/:id/", async (req, res) => {
let {ID} = req.params;
Listing.findByIdAndDelete(ID, {...req.body.listing})
});
app.listen(PORT, () => {
console.log(`server is listening to port ${PORT}`);
})