-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (35 loc) · 793 Bytes
/
index.js
File metadata and controls
41 lines (35 loc) · 793 Bytes
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 app = express();
const port = 3000;
const cors = require("cors");
app.use(express.json());
app.use(cors());
const books = [];
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/books", (req, res) => {
res.json({
books,
});
});
app.post("/books", (req, res) => {
const newBook = req.body;
if (!newBook?.title || !newBook?.author) {
res.status(400).json({
error: "Please provide title and author both",
});
return;
}
const bookToCreate = {
id: Date.now().toString(),
...newBook,
};
books.push(bookToCreate);
res.json({
book: bookToCreate,
});
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});