Question

!! Express.js quesiton !! I just started studying about Node.js with Express.js framework. I tried so...

!! Express.js quesiton !!

I just started studying about Node.js with Express.js framework. I tried so hard but I have no idea how to solve these questions... I hope you guys can help me to solve those questions. Thank you :)

This is my problem :

  1. DELETE /book?title=:book_title

    • always returns 204 Not Content status code even if the book is not present in the datastore.
    • no response body.
  2. GET /books

    • returns 200 Success status code.
    • returns list of all books.
    • return format must be JSON: { "books": [{ "author":"joe", "title":"MyBook", "pages":234, "quality":"new"}, ... ] }
  3. PUT /book

    • updates a single book on the list based on the title.
    • returns 200 Success status code when it finds the book and updates it successfully.
    • returns a single book formatted in JSON as the response body: ex. { "book": { "author":"joe", "title":"MyBook", "pages":234, "quality":"new" }} as long the body has the book and all its fields, the format does not matter.
    • for the PUT, please have your application accept a request body of content-type JSON and treat the title as the identifier when looking for the book for example, your cURL might look like: curl localhost:3000/book -X PUT -d '{"title":"...", "author":"...", ... }' -H 'content-type: application/json'
    • the request body can contain any of the attributes of a book, so if a matching attribute has a new value in the request body, update it, otherwise, ignore it for example, if the request body is {"title":"currentTitle", "author":"newtitle","nonexistentattribute":"nonexistentvalue"}, the datastore would update the book titled "currentTitle" with the new "author" value, but ignore the "nonexistentattribute" since that doesn't exist as an attribute for a book.
    • if the title is not found in the in-memory datastore, return a 404 status response code and whatever body you want to the user.

This is what I did so far(please fix the code..) :

const express = require('express');
const app = express();

app.get("/", (req, res) => {
res.send("Hi, This is Yi Wan's workpage")
});

var books = {
"books": [{
"author": "joe",
"title": "MyBook",
"pages": 234,
"quality": "new"
},
{
"author": "kevin",
"title": "YourBook",
"pages": 432,
"quality": "old"
}
]
}

app.delete('/book?title=:book_title', (req, res) => {
res.send(req.params.book_title);
res.status(204);
});

app.get('/books', (req, res) => {
res.status(200).send(JSON.stringify(books));
});

app.put('/book', (req, res) => {
// have no idea..
});


app.listen(3000, () => console.log("listening on port 3000"));

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have solved your DELETE and PUT methods and you can find the code with useful comments and the screenshots below.

CODE:

const express = require("express");
const app = express();

// Used to get json response body
app.use(express.json());

var books = [
{
author: "joe",
title: "MyBook",
pages: 234,
quality: "new"
},
{
author: "kevin",
title: "YourBook",
pages: 432,
quality: "old"
}
];

app.get("/", (req, res) => {
res.send("Hi, This is Yi Wan's workpage");
});

app.delete("/book/:book_title", (req, res) => {
const title = req.params.book_title;
// Filter books by book title
books = books.filter(book => book.title !== title);
res.sendStatus(204);
});

app.get("/books", (req, res) => {
res.status(200).send(JSON.stringify(books));
});

app.put("/book", (req, res) => {
const data = req.body;
// Missing data by user
if (!data) {
return res.status(404).send("Missing book information.");
}
// Finding book in the datastore
let book = books.filter(book => book.title === data.title);
if (!book.length) {
return res.status(404).send("Book not found.");
}
// map through books data store
books = books.map(book => {
// Check if book found
if (book.title === data.title) {
// Check if author field is present
if (data.author) {
// if new data is available then replace it with the old book data
book.author = book.author === data.author ? book.author : data.author;
}
// Check if author field is pages
if (data.pages) {
book.pages = book.pages === data.pages ? book.pages : data.pages;
}
// Check if author field is quality
if (data.quality) {
book.quality =
book.quality === data.quality ? book.quality : data.quality;
}
}
return book;
});
return res.status(200).send("Book updated successfully.");
});

app.listen(3000, () => console.log("listening on port 3000"));

IMAGES

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
!! Express.js quesiton !! I just started studying about Node.js with Express.js framework. I tried so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is the code I have for the above picture. I have to pull the Best...

    This is the code I have for the above picture. I have to pull the Best Sellers List of fiction and non fiction books from the New York Times API and parse the JSON response. <!DOCTYPE html> <html> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <ul> <li ng-repeat="x in books"> {{ x.title + ', ' + x.author }} </li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("http://api.nytimes.com/svc/books/v3/lists/.json?api-key=37775f104c1d0455b95606647354d2ae:10:74362200") .then(function (response) { alert("RESPONSE=" + response.data.results.book[0].author);     $scope.books = response.data.results.book; });...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • Can somebody help me with the Use Case Diagram . I am confused of what I...

    Can somebody help me with the Use Case Diagram . I am confused of what I am suppose to do. Here are the instructions : Your team should produce a Use Case Diagram and the associated Use Case Descriptions/Narratives for all the use cases in the diagram. The resulting document should havethe “professional look” and produced by a word processor, graphics/presentation/drawing software, and/or a CASE tool (e.g., Microsoft Word, Microsoft PowerPoint, ArgoUML, Dia, Visual Paradigm, Visio, etc.). All project documentation...

  • I cant complete this assignment, I have tried several ways shown in the tutorial but cant...

    I cant complete this assignment, I have tried several ways shown in the tutorial but cant seem to get it to validate, can anyone help? This is a basic JavaScript tutorial and will not validate with Jquery. Monroe Public Library Denise Kruschev manages the website for the Monroe Public Library in Monroe, Ohio. One of her responsibilities is to add content and links to the site that will be of interest to the library’s patrons. Denise has asked you to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT