-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (35 loc) · 1.05 KB
/
Copy pathapp.js
File metadata and controls
46 lines (35 loc) · 1.05 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
import express, { query } from "express"
import { deleteBook, getAllBooks, getBookById, insertBook } from "./model/books.js"
const PORT = 3000
const app = express()
app.use(express.json());
app.use( function(req, res, next){
console.log("Welcome to my App")
next()
})
app.get("/", function(req, res){
res.json({Messa: "We are here"})
})
app.get("/books", async function(req, res){
const payload = await getAllBooks()
res.json(payload)
})
app.get("/books/:id", async function(req, res){
const id = req.params.id
const payload = await getBookById(id)
res.json(payload)
})
app.post("/books", async function(req, res){
const title = req.body.title
const author = req.body.author
const book = await insertBook(title, author)
res.json({message: "Added", book})
})
app.delete("/books/:id", async function(req, res){
const id = req.params.id
const payload = await deleteBook(id)
res.json({message: "Book deleted", payload})
})
app.listen(PORT, function(req, res){
console.log("Up and listening on Port 300")
})