Book directory project
Asked by Wasim Raja about 2 years ago
0
1const router = require('express').Router() 2const books = require('./data') 3 4let booksDirectory = books 5 6router.get('/reset', function (req, res) { 7 booksDirectory = books 8 res.send('ok') 9}) 10 11router.get('/books', function (req, res) { 12 res.send(booksDirectory) 13}) 14 15router.delete('/books/:id', function (req, res) { 16 const { id } = req.params; 17 booksDirectory.forEach((book, index) => { 18 if (book.id === parseInt(id)) { 19 booksDirectory.splice(index); 20 } 21 }); 22 res.json({ message: `Book with id #${id} has been deleted` }); 23}) 24 25router.get('/books/:id', function (req, res) { 26 const { id } = req.params; 27 res.json(booksDirectory.filter((ele) => ele.isbn === parseInt(id))); 28}) 29 30router.post('/books', function (req, res) { 31 const { 32 title, 33 isbn, 34 pageCount, 35 publishedDate, 36 thumbnailUrl, 37 shortDescription, 38 longDescription, 39 status, 40 authors, 41 categories, 42 } = req.body 43 booksDirectory.push({ 44 title, 45 isbn, 46 pageCount, 47 publishedDate, 48 thumbnailUrl, 49 shortDescription, 50 longDescription, 51 status, 52 authors, 53 categories, 54 }) 55 56}) 57 58router.put('/books/:id', function (req, res) { 59 const { id } = req.params 60 const { 61 title, 62 isbn, 63 pageCount, 64 publishedDate, 65 thumbnailUrl, 66 shortDescription, 67 longDescription, 68 status, 69 authors, 70 categories, 71 } = req.body 72 booksDirectory.forEach((book, index) => { 73 if (book.id === parseInt(id)) { 74 booksDirectory[index] = { 75 title, 76 isbn, 77 pageCount, 78 publishedDate, 79 thumbnailUrl, 80 shortDescription, 81 longDescription, 82 status, 83 authors, 84 categories, 85 }; 86 } 87 }); 88 res.json({ message: `The book with ID ${id} has been updated` }); 89}) 90 91 92 93module.exports = router``` 94 95what are the mistakes ?
2 Answers
1
You can check the verified solution of the lab now by clicking on "Stuck? Check Solution" button here: https://codedamn.com/learn/nodejs-fundamentals/servers-in-node-js/book-directory-project-with-node-js.sQa0e6-mhS441ZJTYKPdW
0
What you are trying to get?
show more answers
Your answer