【发布时间】:2022-01-23 10:45:14
【问题描述】:
我有一些代码如下
// [DELETE] /api/v1/authors/:id
async deleteAuthor(req, res) {
const author = await Author.findByIdAndRemove(req.params.id);
// delete blogs of the author
axios.delete(
`http://localhost:${process.env.PORT}/api/v1/blogs/author/${author._id}`,
{
headers: {
Authorization: req.headers.authorization
}
}
);
res.status(200).send();
}
借此,我想删除一位作者及其所有博客。我知道 uri 的命名不好,但总体而言,这样的编码是一种好方法,还是有其他方法可以做同样的事情。我正在使用 Node.js 和 Mongoose
【问题讨论】:
-
通常最好调用一个内部函数来执行操作并且不通过你的 HTTP 层(因为这里的 HTTP 部分只是浪费和不必要的,因为你已经是对的了处理并有权访问您的所有共享代码)。您可以在多个路由中使用相同的内部共享函数。
标签: node.js mongodb mongoose axios