-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (41 loc) · 1.24 KB
/
index.js
File metadata and controls
48 lines (41 loc) · 1.24 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
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const port = 3000;
app.use(express.static(path.join(__dirname, "frontend/build")));
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/db", {
auth: { authSource: "admin" },
user: "root",
pass: "admin",
useNewUrlParser: true,
useUnifiedTopology: true,
});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function () {
console.log("Db connected.");
});
var bookReviewSchema = new mongoose.Schema({
name: String,
rating: Number,
reviewer: String,
});
var BookReview = mongoose.model("BookReview", bookReviewSchema);
app.get("/bookReviews", async (req, res) => {
console.log("GET /bookReviews");
const all = await BookReview.find();
res.send(all);
});
app.post("/bookReviews", async (req, res) => {
console.log("POST /bookReviews", req.body);
const review = new BookReview(req.body);
let item = await review.save();
res.send(item);
});
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);