连接 MongoDB 到 Koa 服务器问题
我已经在我的 Koa 服务器上沉寂了一段时间了。看起来我已经很接近了,但我似乎无法完全完成它。
每当我想向消息数据库发布某些内容时,它就会崩溃并显示拓扑已关闭。
我目前正在通过邮递员添加非常基本的 json 文件,其中包含 msg 和 id 属性,希望更有经验的人可以帮助我解决这个问题。肯定会非常感激。
const Koa = require("koa");
const bodyparser = require("koa-bodyparser");
const KoaRouter = require("koa-router");
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017/';
const app = new Koa();
const router = new KoaRouter();
const client = new MongoClient(url);
const db = client.db('serverTesting');
const messages = db.collection("messsages")
const PORT = 3002;
app.use(bodyparser())
// router
app.use(router.routes());
router.get("/", ctx => {
ctx.body = "Hello, this is the Homepage.";
});
router.get("/test", ctx => {
ctx.body = { msg: "Hello Test"};
});
router.post("/test", ctx => {
const message = ctx.request.body;
messages.insertOne(message);
ctx.body = "Message was updated."
console.log("Added message to database: ")
console.log(messages);
});
router.delete("/test", ctx => {
const msg = ctx.request.body.msg;
const deleted = messages.find(message => message.msg === msg);
if (deleted) {
messages = messages.filter(message => message.msg !== msg);
console.log("Deleted message from database: ")
console.log(messages)
ctx.body = "Message was deleted."
} else {
ctx.body = "This message was not found in the database"
}
})
router.put("/test", ctx => {
const id = ctx.request.body.id;
const deleted = messages.find(message => message.id === id);
if (deleted) {
messages.forEach(message => {
if (message.id === id) {
message.msg = ctx.request.body.msg
}
})
console.log("Updated message in database: ")
console.log(messages)
ctx.body = "Message was updated."
} else {
ctx.body = "This message was not found in the database"
}
})
app.listen(PORT, () => {
client.connect()
console.log(`Server running on http://localhost:${PORT}
I have been stuck on my Koa server for quiet a while now. It seems like I'm very close, I just can't seem to fully finish it.
Whenever I want to post something to my messages DB, it crashes and says Topology is closed.
I'm currently adding very basic json files through postman with a msg and id property, hope someone more experienced can help me iron this out. Would definitely be much appreciated.
const Koa = require("koa");
const bodyparser = require("koa-bodyparser");
const KoaRouter = require("koa-router");
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017/';
const app = new Koa();
const router = new KoaRouter();
const client = new MongoClient(url);
const db = client.db('serverTesting');
const messages = db.collection("messsages")
const PORT = 3002;
app.use(bodyparser())
// router
app.use(router.routes());
router.get("/", ctx => {
ctx.body = "Hello, this is the Homepage.";
});
router.get("/test", ctx => {
ctx.body = { msg: "Hello Test"};
});
router.post("/test", ctx => {
const message = ctx.request.body;
messages.insertOne(message);
ctx.body = "Message was updated."
console.log("Added message to database: ")
console.log(messages);
});
router.delete("/test", ctx => {
const msg = ctx.request.body.msg;
const deleted = messages.find(message => message.msg === msg);
if (deleted) {
messages = messages.filter(message => message.msg !== msg);
console.log("Deleted message from database: ")
console.log(messages)
ctx.body = "Message was deleted."
} else {
ctx.body = "This message was not found in the database"
}
})
router.put("/test", ctx => {
const id = ctx.request.body.id;
const deleted = messages.find(message => message.id === id);
if (deleted) {
messages.forEach(message => {
if (message.id === id) {
message.msg = ctx.request.body.msg
}
})
console.log("Updated message in database: ")
console.log(messages)
ctx.body = "Message was updated."
} else {
ctx.body = "This message was not found in the database"
}
})
app.listen(PORT, () => {
client.connect()
console.log(`Server running on http://localhost:${PORT} ????`);
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全知道您的代码问题是什么,因为它对我有用,也许与您的数据库设置有关。
我建议一个有效的例子,您可以自己尝试。
我假设您有 docker 和 curl 在您的系统中。
docker-compose.yaml
create docker-compose.yaml and 和运行
Docker组成
。这将使Mongo Express在8077
端口上运行,并且DB本身在27077
上。index.js
I don't exactly know what the issue is with your code because it worked for me, perhaps it has something to do with your DB setup.
I'm suggesting a working example and you can try it on your own.
I assume you have Docker and Curl in your system.
docker-compose.yaml
Create
docker-compose.yaml
and rundocker compose up
. This will make Mongo express running on8077
port and the DB itself on27077
.index.js
Run the server with
node index.js
and send the payload to test itYou can navigate to http://localhost:8077/ and check the created value.