-
Initial steps to create api, which runs in node.
-
npm init -y
creates package.json -
download express frameword
npm i express
-
echo node_modules >> .gitignore
creates .gitignore files and write node_modules -
create a application folder mkdir app with index.js file.
-
Install the Dev dependencies
npm i -D nodemon
npm i morgan joi dotenv uuid
install the dependencies.- morgan prints the http log in the terminal
- joi used to validate the user data given to the api.
- dotenv used to maintain the secret files that should not pushed to the git repo, confidential datas.
- uuid used to create unique id.
- change the script in package.json to "start": "node app" and "dev": "nodemon"
-
creating port in .env .env is created in the main folder and it must be included in the .gitignore file.
- get the array of objects with id, title and isbn.
- adding movie with title and isbn with validaton for isbn-10
- error handler for the next(arg)
add rating within the range 0 to 5 only add rating, if it is not entered.
const rating = [
{
rateId,
rating,
bookId,
}
]
find the rating provided for the book, then display the single book with rating.
output of the single book
{
"id": 1234,
"title": "Storywallah",
"isbn": "0143445774",
"ratings": 5
}
by default rating should be 0.
{ title: '' } no isbn should be passed
deleting the book with id
{ rating: 0 and 5, }
//updating single book with rating
const updateRating = ({ bookId, rating }) => {
const idx = books.findIndex((b)=> b.id == bookId);
if(idx == -1){
return null;
}
rating && (ratings[idx]['rating'] = rating);
return rating[idx];
};
if rating is not found for a book, return error saying so
{ id: 100, rating: 3, book: { id: 216, title: '', isbn: '', } }