Rid i books:nodejsDELETEauthorandGETbookstoshowallbooks withinfo

第一次尝试 node 和 express。我使用 get 和 post 的例子,我需要介绍 DELETE 删除基于标题的书,我是另一个 GET,以便在键入“localhost:3000 / books”时显示所有书籍。

这是我到目前为止

    var express = require('express');
var app = express();
var bodyPr = require("body-pr");
const { fstat } = require('fs');
// adding a body pr to handle JSON and url encoded form data for us automagically
app.use(bodyPr.json());
app.use(bodyPr.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;
        }
    }
    // p 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");
});

我有这个删除只是不知道它是否会工作。我现在还在玩邮差

    // delete
app.delete('/delete/:booke', function(req, res) {
    var id = req.params.book;
    fstat.readFile("./book.json", 'utf8', (err,data) => {
        data = JSON.p( data );
        delete data["book" + title];
        console.log( JSON.stringify(data) );
        res.status(200);
        return res.send("Book removed");
    });
})

任何帮助将是巨大的。

1

不要对数组使用delete关键字,它只会清空元素,同时保留相同的数组长度。

此外,您希望从books中删除,而不是从data中删除。

如果bookeURL 段(其中有一个错字)应该包含要删除的索引,则代码为:

books.splice(id, 1);

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(432)
Coin telegraph:电报API 上传文件(telegraph gif)
上一篇
Css项目:Codepen:项目-CSS不工作
下一篇

相关推荐

  • Coin telegraph:电报API 上传文件(telegraph gif)

    关于Coin telegraph的问题,在telegraph gif中经常遇到,我试图通过它的 API 上传一个文件,这是一个图像到电报页面。如果我发送一个链接,一个 URL 到已经上传到网络的文件,一切工作正常。…

    2022-11-23 08:30:33
    0 12 84
  • css底部对齐:Welcome to Our Website

    示例示例css底部对齐指的是将多个元素的底部对齐,使用CSS实现底部对齐可以使用flex布局。代码示例:…

    2023-04-04 16:01:40
    0 16 14
  • css透明色:The Beauty of Transparent Colors

    示例示例CSS透明色是指使用CSS定义的颜色,其中包含一定的不透明度。它可以让你的网页上的元素部分透明,而不会影响其他元素的外观。CSS透明色可以使用rgba()函数来定义,rgba()函数接受4个参数,分别是红色(red)、绿色(green)、蓝色(blue)和alpha值,alpha值表示透明度。…

    2023-03-18 06:00:19
    0 70 18
  • css cursor属性:探索CSS Cursor属性,让你的页面更有交互性

    示例示例CSS cursor属性是用来设置鼠标指针的形状。它可以接受多种值,包括指针,文本,加号,减号,手型等。代码示例:…

    2023-02-14 11:20:49
    0 33 91
  • css3 文字渐变:CSS3 Text Gradient

    CSS3 文字渐变是一种使用 CSS3 来给文字添加渐变效果的技术,它利用了 CSS3 的 background-image 属性,通过设置 linear-gradient() 来实现渐变效果。…

    2023-01-27 14:07:45
    0 95 37
  • canvas基础:var canvas = document.getElementById('canvas');var ctx

    Canvas 是 HTML5 中新增的一个元素,它可以使用 JavaScript 在网页上绘制图形。Canvas 的基本原理是,在一个指定的区域内,使用 JavaScript 绘制图形,从而达到动态生成图像的效果。…

    2023-01-15 03:58:44
    0 80 30
  • css文本不换行 nowrap;

    CSS代码:说明:…

    2023-01-19 14:51:46
    0 59 38
  • css设置placeholder:Enter Your Text Here

    CSS设置,可以使用CSS伪元素`。`来实现,代码如下:上面的代码中,我们使用`。`来设置占位符的字体颜色和字体大小,可以根据需要进行调整。…

    2023-04-03 11:45:09
    0 23 46

发表评论

登录 后才能评论

评论列表(43条)