安装mongodb 官网mongodb 中文官网node 实例: 使用 express 和 mongoose 实现简单的文章管理 API初始化项目bash自动换行复制npm init -y 依赖安装bash自动换行复制npm i express@latest cors mongoose 数据建模js自动换行复制import mongoose from "mongoose" const Article = mongoose.model( "Article", new mongoose.Schema({ title: { type: String }, }) ) 增删改查 API 实现获取列表js自动换行复制app.get("/articles", async (req, res) => { const article = await Article.find() res.send({ success: true, message: "get article list", data: article, }) }) 根据 id 获取详情js自动换行复制app.get("/articles/:id", async (req, res) => { const id = req.params.id const article = await Article.findById(id) res.send({ success: true, message: "get article by id", data: article, }) }) 创建文章js自动换行复制app.post("/articles", async (req, res) => { const body = req.body const article = await Article.create({ title: body.title, }) res.send({ success: true, message: "create article", data: article, }) }) 编辑文章js自动换行复制app.put("/articles/:id", async (req, res) => { const id = req.params.id const body = req.body const article = await Article.findById(id) article.title = body.title await article.save() res.send({ success: true, message: "update article", data: article, }) }) 删除文章js自动换行复制app.delete("/articles/:id", async (req, res) => { const id = req.params.id await Article.deleteOne({ _id: id }) // 或者可以使用以下方法进行删除 // const article = await Article.findById(id) // await article.remove() res.send({ success: true, message: "delete article", }) }) 完整代码js自动换行复制import cors from "cors" import express from "express" import mongoose from "mongoose" const app = express() app.use(express.json()) app.use(cors()) try { await mongoose.connect("mongodb://127.0.0.1:27017/mongodb-test") } catch (error) { console.error("error:数据库连接失败\n", error) } const Article = mongoose.model( "Article", new mongoose.Schema({ title: { type: String }, }) ) app.get("/articles", async (req, res) => { const article = await Article.find() res.send({ success: true, message: "get article list", data: article, }) }) app.get("/articles/:id", async (req, res) => { const id = req.params.id const article = await Article.findById(id) res.send({ success: true, message: "get article by id", data: article, }) }) app.post("/articles", async (req, res) => { const body = req.body const article = await Article.create({ title: body.title, }) res.send({ success: true, message: "create article", data: article, }) }) app.put("/articles/:id", async (req, res) => { const id = req.params.id const body = req.body const article = await Article.findById(id) article.title = body.title await article.save() res.send({ success: true, message: "update article", data: article, }) }) app.delete("/articles/:id", async (req, res) => { const id = req.params.id await Article.deleteOne({ _id: id }) res.send({ success: true, message: "delete article", }) }) const port = 3000 app.listen(port, () => { console.log(`http://localhost:${port}`) }) 84 行查看运行情况可以使用 vscode 插件 rest client 来测试接口运行情况,以下是我本地接口测试文件。如果你要在本地测试,可以将 66ae4047563fa3933c1b38ba 改成真实的查询列表里的某一个文章的 id 即可提醒rest client 这个插件在我的博客里有介绍,可以移步 rest client 插件使用 了解其基础使用方法测试代码http自动换行复制@uri=http://localhost:3000 ### 获取文章列表 GET {{uri}}/articles ### 创建文章 POST {{uri}}/articles Content-Type: application/json { "title": "添加文章标题" } ### 根据id获取文章 GET {{uri}}/articles/66ae4047563fa3933c1b38ba ### 编辑文章 PUT {{uri}}/articles/66ae4047563fa3933c1b38ba Content-Type: application/json { "title": "编辑文章标题" } ### 删除文章 DELETE {{uri}}/articles/66ae4047563fa3933c1b38ba 27 行
评论区
评论加载中...