-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (53 loc) · 1.69 KB
/
index.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
67
68
69
70
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const { MongoClient, ObjectId } = require("mongodb");
(async () => {
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASSWORD;
const dbHost = process.env.DB_HOST;
const dbName = process.env.DB_NAME;
const url = `mongodb+srv://${dbUser}:${dbPassword}@${dbHost}/${dbName}?retryWrites=true&w=majority`;
const client = await MongoClient.connect(url, { useUnifiedTopology: true });
const db = client.db(dbName);
const app = express();
app.use(bodyParser.json());
const port = process.env.PORT || 3000;
/*
Create, Read All/Single, Update & Delete
*/
const books = db.collection("books");
app.get("/", (req, res) => {
res.send("BOOKS-API is running...");
});
//Create --
app.post("/books", async (req, res) => {
const book = req.body;
await books.insertOne(book);
res.status(201).send(book);
});
//Read All --
app.get("/books", async (req, res) => {
res.send(await books.find().toArray());
});
//Read Single --
app.get("/books/:id", async (req, res) => {
const { id } = req.params;
const book = await books.findOne({ _id: ObjectId(id) });
res.send(book);
});
//Update --
app.put("/books/:id", async (req, res) => {
const { id } = req.params;
const book = req.body;
await books.updateOne({ _id: ObjectId(id) }, { $set: book });
res.json({ message: "Book updated!" });
});
//Delete --
app.delete("/books/:id", async (req, res) => {
const { id } = req.params;
await books.deleteOne({ _id: ObjectId(id) });
res.json({ message: "Book deleted!" });
});
app.listen(port);
})();