-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (35 loc) · 1.03 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
const express = require("express");
const { resolve } = require("path");
const { getAllBooks, getBookById, addBook } = require("./book.js");
const app = express();
const port = 3010;
app.use(express.static("static"));
app.use(express.json());
app.get("/", (req, res) => {
res.sendFile(resolve(__dirname, "pages/index.html"));
});
function validateBook(book) {
if (!book) {
return "Book data is invalid!";
} else if (!book.title || typeof book.title != "string") {
return "Book title is invalid!";
} else if (!book.author || typeof book.author != "string") {
return "Book author is invalid!";
} else {
return null;
}
}
app.post("/api/book/new", async (req, res) => {
const book = req.body;
const error = validateBook(book);
if (error) {
return res.status(400).json({ error: error });
}
try {
const result = await addBook(book);
return res.status(201).json(result);
} catch (error) {
return res.status(500).json({ error: "Internal Server Error" });
}
});
module.exports = { app, validateBook };