Express.js 删除请求
Express.js delete request
正在制作 MERN-stack 应用程序,但删除请求功能不起作用。
这是相关代码
尝试使用 Postman 发送删除请求时显示此错误。
我搜索了其他一些 Whosebug 问题,但找不到答案。
在我之前的快递申请中,它的效果非常好。
无法删除/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 所以真正的 link 应该像 DELETE /api/todos/{id},例如 /api/todos/3
我注意到你的请求被发送到 /api/todos 没有参数
正在制作 MERN-stack 应用程序,但删除请求功能不起作用。
这是相关代码
尝试使用 Postman 发送删除请求时显示此错误。 我搜索了其他一些 Whosebug 问题,但找不到答案。 在我之前的快递申请中,它的效果非常好。
无法删除/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 所以真正的 link 应该像 DELETE /api/todos/{id},例如 /api/todos/3
我注意到你的请求被发送到 /api/todos 没有参数