express - 为什么评论一直处于待处理状态?
问题描述
我为一个小型博客应用程序开发了一个微服务应用程序。它具有发布服务、评论服务、查询服务和审核服务。
审核服务负责审核和过滤某些评论并批准其他评论:
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const app = express();
app.use(bodyParser.json());
app.post("/events", async (req, res) => {
const { type, data } = req.body;
if (type === "CommentCreated") {
const status = /cdc/gi.test(data.content) ? "rejected" : "approved";
await axios.post("http://localhost:4005/events", {
type: "CommentModerated",
data: {
id: data.id,
postId: data.postId,
status,
content: data.content,
},
});
}
res.send({});
});
app.listen(4003, () => {
console.log("Listening on 4003");
});
在一些手动测试期间,我关闭了查询服务并创建了一个带有评论的帖子,期望一旦查询服务重新上线,它将接收从事件总线创建的帖子和评论的事件,用户将能够看到已审核的评论,即那些被批准和拒绝的评论,但我相信我在我的应用程序中引入了一个错误,因为评论服务中默认评论的状态为待处理,如下所示:
const express = require("express");
const bodyParser = require("body-parser");
const { randomBytes } = require("crypto");
const cors = require("cors");
const axios = require("axios");
const app = express();
app.use(bodyParser.json());
app.use(cors());
const commentsByPostId = {};
app.get("/posts/:id/comments", (req, res) => {
res.send(commentsByPostId[req.params.id] || []);
});
app.post("/posts/:id/comments", async (req, res) => {
const commentId = randomBytes(4).toString("hex");
const { content } = req.body;
const comments = commentsByPostId[req.params.id] || [];
comments.push({ id: commentId, content, status: "pending" });
commentsByPostId[req.params.id] = comments;
await axios.post("http://localhost:4005/events", {
type: "CommentCreated",
data: { id: commentId, content, postId: req.params.id, status: "pending" },
});
res.status(201).send(comments);
});
app.post("/events", async (req, res) => {
console.log("Event Received:", req.body.type);
const { type, data } = req.body;
if (type === "CommentModerated") {
const { id, postId, status, content } = data;
const comments = commentsByPostId[postId];
const comment = comments.find((comment) => {
return comment.id === id;
});
comment.status = status;
await axios.post("http://localhost:4005/events", {
type: "CommentUpdated",
data: {
id,
content,
postId,
status,
},
});
}
res.send({});
});
我不清楚如何摆脱我画的这个角落,status: 'pending'
将其设置为默认行为,因为我没想到当微服务因崩溃或其他原因重新上线时,它会停留在这种默认行为中。
解决方案
问题出在我的事件总线中,因为我早些时候在我的事件总线终端中不断收到未处理的承诺拒绝,我认为我必须在其中添加async/await
语法,如下所示:
app.post("/events", async (req, res) => {
const event = req.body;
events.push(event);
await axios.post("http://localhost:4000/events", event);
await axios.post("http://localhost:4001/events", event);
await axios.post("http://localhost:4002/events", event);
await axios.post("http://localhost:4003/events", event);
res.send({ status: "OK" });
});
但最后我意识到我仍然在我的事件总线终端中收到未处理的承诺拒绝,并且它没有影响其他任何东西,最初我认为上述操作并不是一个异步操作。
因此,在仔细研究了包括前端代码库在内的所有代码之后,我凭直觉决定回到事件总线并删除async/await
语法,果然,当查询服务关闭时,我能够提交评论,当查询服务出现时回到网上,它不仅收到了CommentCreated
事件,还收到了CommentModerated
和CommentUpdated
事件。
所以它应该是这样的:
app.post("/events", (req, res) => {
const event = req.body;
events.push(event);
axios.post("http://localhost:4000/events", event);
axios.post("http://localhost:4001/events", event);
axios.post("http://localhost:4002/events", event);
axios.post("http://localhost:4003/events", event);
res.send({ status: "OK" });
});
推荐阅读
- java - 如何使用 VirtualSticks 实际控制无人机
- java - HashMap的HashMap->输出问题
- expo - 无论 initialRegion 参数如何,MapView 都会在赤道上检索非洲地图?
- python - 如果使用 python 和 beautifulsoup 在网页中找到部分匹配,则显示整个字符串
- redux - 关闭浏览器后如何保持状态
- python - 多对多字段不显示
- jquery - 在 vue js 中使用 Jquery 数据表
- apache-kafka - 我可以创建一个 ISR 低于复制因子的 Kafka 主题吗?
- spring-boot - 无法绕过 javax.net.ssl.SSLHandshakeException:一般 SSLEngine 问题
- visual-studio - Visual Studio:远程构建不断复制所有文件