From IBM on Coursera
In this final project, we will build a server-side online book review application and integrate it with a secure REST API server which will use authentication at session level using JWT. You will then test your application using Promises callbacks or Async-Await functions.
npm install
npm start
curl -i localhost:5000/
curl -i localhost:5000/isbn/1
curl -i localhost:5000/author/Unknown
curl -i localhost:5000/title/Fairy%20tales
curl -i localhost:5000/review/6
curl --header "Content-Type: application/json" \
--request POST \
--data '{ "username":"john", "password":"fake" }' \
localhost:5000/register
curl --header "Content-Type: application/json" \
--request POST \
--data '{ "username":"john", "password":"fake" }' \
localhost:5000/customer/login
For this part it is necessary that the app is accessible from the Internet via the free Postman account that you have created, since
curl
cannot accessreq.session
from CLI
PUT <labURL>/customer/auth/review/1?review=good
DELETE <labURL>/customer/auth/review/1
// Example tested successfully (but not kept in general.js)
public.get('/', async function(req, res) {
return await search('/', null, res)
});
// Example tested successfully (but not modified in general.js)
public.get('/isbn/:isbn', function(req, res) {
const request = new Promise((resolve) => {
(async function(){
const response = await search('isbn', req.params['isbn'], res);
resolve(response)
})()
});
request.then(result => { return result })
});
db connection not provided in the course
function search(col, it, res) {
if (col === '/') return res.status(200).json({ books: db });
else if (col === 'review' && db.hasOwnProperty(it))
return res.status(200).json(db[it].reviews);
else {
let result = {};
if (col === 'isbn') Object.keys(db)
.filter(id => String(id).indexOf(it) > -1)
.forEach(id => result[id] = db[id])
;
else {
for (const id in db) {
if (Object.hasOwnProperty.call(db, id)) {
if (String(db[id][col]).indexOf(it) > -1) {
result[id] = db[id]
}
}
}
}
if (Object.keys(result).length)
return res.status(200).json(result);
else return res.status(404).json({ message: 'Not Found' })
}
}