简体   繁体   中英

node js DELETE author and GET books to show all books with info

First time trying node and express. The example I have use get and post, I would need to introduce DELETE to delete a book based on the title and I'm another GET in order to display all books when "localhost:3000/books" is typed.

This is what I have so far

    var express = require('express');
var app = express();
var bodyParser = require("body-parser");
const { fstat } = require('fs');

// adding a body parser to handle JSON and url encoded form data for us automagically
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// our in-memory fake data store is just an array of javascript objects
var books = [
    { "author": "me", "title": "BookA", "pages": 600, "quality": "new" },
    { "author": "you", "title": "BookB", "pages": 400, "quality": "used" },
    { "author": "us", "title": "BookC", "pages": 500, "quality": "old" },
];

// request handler to search based on two fields: author and title
app.get('/book', function (req, res) {
    // get the query params
    var title = req.query.title;

    // initialize the return data
    var data;

    // search for the book
    for (var i = 0; i < books.length; i++) {
        if (books[i].title == title) {
            data = books[i];
            break;
        }
    }

    // pass JSON back to client
    res.set('Content-type', 'application/json');
    res.status(200);
    res.send({"book": data});
});

// post handler to add a book to the hardcoded list
// this is definitely not the correct model for storing data
// this is simply an example
app.post('/book', function (req, res) {
    // access the request POST body from the request object
    var data = req.body.data;

    // add the new book to the data store and return it
    var book = {
        "author": req.body.author,
        "title": req.body.title,
        "pages": req.body.pages,
        "quality": req.body.quality,
    }

    // add the book to the hardcoded list of books
    books.push(book);

    // return JSON list of books
    res.set('Content-type', 'application/json');
    res.status(201);
    res.send({"books": books});
});

// listen for HTTP requests on port 3000
app.listen(3000, function() {
    console.log("listening on port 3000");
});

I have this for DELETE just not sure if it'll work. I'm still playing around with POSTMAN at the moment

    // delete
app.delete('/delete/:booke', function(req, res) {
    var id = req.params.book;
    fstat.readFile("./book.json", 'utf8', (err,data) => {
        data = JSON.parse( data );

        delete data["book" + title];

        console.log( JSON.stringify(data) );
        res.status(200);

        return res.send("Book removed");
    });
})

Any help would be great.

Do not use the delete keyword for an Array, it will just empty the element while retaining the same array length.

Moreover, you want to delete from books , not data .

If the booke URL segment (in which you have a typo) is supposed to contain the index to delete, the code is:

books.splice(id, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM