【问题标题】:I have a question about solving this express.js problems我有一个关于解决这个 express.js 问题的问题
【发布时间】:2020-03-19 17:51:00
【问题描述】:

我刚开始研究 Node.js 和 Express.js 框架。 我很努力,但我不知道如何解决这些问题......我希望你们能帮助我解决这些问题。谢谢你:)

这是我的问题:

  1. 删除 /book?title=:book_title

    • 即使图书不在数据存储区中,也始终返回 204 Not Content 状态代码。
    • 没有响应正文。
  2. 获取/书籍

    • 返回 200 成功状态码。
    • 返回所有书籍的列表。
    • 返回格式必须是 JSON: { "books": [{ "author":"joe", "title":"MyBook", "pages":234, "quality":"new"}, ... ] }
  3. PUT /书

    • 根据书名更新列表中的一本书。
    • 找到图书并成功更新时返回 200 Success 状态码。
    • 返回以 JSON 格式的单本书作为响应正文: 前任。 {“书”:{“作者”:“乔”,“标题”:“MyBook”,“页面”:234,“质量”:“新”}} 只要正文包含书籍及其所有字段,格式就无关紧要。
    • 对于 PUT,请让您的应用程序接受内容类型 JSON 的请求正文,并在查找图书时将标题视为标识符 例如,您的 cURL 可能如下所示: curl localhost:3000/book -X PUT -d '{"title":"...", "author":"...", ... }' -H '内容类型:application/json'
    • 请求正文可以包含书籍的任何属性,因此如果匹配的属性在请求正文中有新值,则更新它,否则忽略它 例如,如果请求正文是 {"title":"currentTitle", "author":"newtitle","nonexistentattribute":"nonexistentvalue"},则数据存储将使用新的“author”更新标题为“currentTitle”的书值,但忽略“不存在的属性”,因为它不作为书籍的属性存在。
    • 如果在内存数据存储中找不到标题,则向用户返回 404 状态响应代码和您想要的任何正文。

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

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"));

【问题讨论】:

  • 不确定 express 是否解析 url 的查询字符串部分以获取路由参数,因为您可以使用 req.query.book_title 来获取它。尝试将其作为路径的一部分,例如前端的 /book/:book_title 网址将类似于 http://example.com/book/somebook 看看这是否有所不同
  • @PatrickEvans 我试过但仍然“无法获取 /book/:book_title”...
  • ...实际上我对实际问题感到困惑...所有要点都是问题列表还是需求列表
  • @PatrickEvans 这是每个号码的所有要求。

标签: javascript node.js express


【解决方案1】:

这里是更新后的代码,可以为您提供所需的结果。我会建议做一些阅读。

阅读:How to create a REST API with Express.js in Node.jsREST API with Node and Express in 5 minutes。也可以查看Express API详细了解。

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

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

// keep this as array of objects
// and not an object of array of objects
// TIP: it's best to keep as object of objects
var books = [{
      "author": "joe",
      "title": "MyBook",
      "pages": 234,
      "quality": "new"
    },
    {
      "author": "kevin",
      "title": "YourBook",
      "pages": 432,
      "quality": "old"
    }
  ]

app.delete('/book/:title', (req, res) => {
  // find the book and delete it
  for (var i = 0; i < books.length; i++) {
    if (books[i].title == req.params.title) {
      books.splice(i, 1);
      res.status(200).end(); // end the response
    }
  }

  // book not found, so send proper error code
  res.status(404).end(); // resource does not exist
});

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

app.put('/book/:title', (req, res) => {
  // find the book and update it
  for (var i = 0; i < books.length; i++) {
    if (books[i].title == req.params.title) {
      books.splice(i, 1); // delete the old book
      books.push(req.body); // add the new book
      res.status(200).end(); // end the response
    }
  }

  // book not found, so send proper error code
  res.status(404).end(); // resource does not exist
});


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2020-07-25
    • 2021-10-09
    • 2021-11-02
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多