!! 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 :
DELETE /book?title=:book_title
GET /books
PUT /book
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"));
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:

!! Express.js quesiton !! I just started studying about Node.js with Express.js framework. I tried so...
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 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 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 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...