繁体   English   中英

Express.js 删除请求

[英]Express.js delete request

制作 MERN 堆栈应用程序,但删除请求 function 不起作用。
这是相关代码

尝试使用 Postman 发送删除请求时,会显示此错误。 我搜索了其他一些 StackOverflow 问题,但找不到答案。 在我以前的快速应用程序中,它就像一个魅力。

无法删除 /api/todos

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <!--This is the error -->
    <pre>Cannot DELETE /api/todos</pre>
    <!--This is the error ^ -->
</body>

</html>

Todos.js


  const express = require('express');
  const uuid = require('uuid');
  const router = express.Router();
  const todos = require('../../Todo');

  router.get('/', (req, res) => {
      res.json(todos);
  });

  router.get('/:id', (req, res) => {
    const found = todos.some(todo => todo.id === req.params.id);

    if (!found) {
      res.status(400).json({ msg: `No meber whit id of ${req.params.id}` });
    } else {
      res.json(todos.filter(todo => todo.id === req.params.id));
    }
  });

  router.post('/', (req, res) => {
    const newEntry = {
      id: uuid.v4(),
      title: req.body.title,
      completed: false,
    };

    if (!req.body.title) {
      res.status(400).json({ msg: `Pleas include title` });
    }

    todos.push(newEntry);
    res.json(todos);
  });

  router.delete('/:id', (req, res) => {
    const found = todos.some(todo => todo.id === req.params.id);

    if (!found) {
      res.status(400).json({ msg: `No meber whit id of ${req.params.id}` });
    } else {
      todos.filter(todo => todo.id !== req.params.id);
      res.json(todos);
    }
  });

  module.exports = router;

router.delete('/:id', (req, res)需要一个参数 id 所以真正的链接应该像 DELETE /api/todos/{id},例如 /api/todos/3

正如我注意到您的请求被发送到 /api/todos 没有参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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