11.Node.js API接口

八、API接口

8.1 json-server工具

1)安装json-server

复制代码
npm i -g json-server

2)示例

json 复制代码
//students.json
{
    "student":[
        {"id":1,"name":"sally","age":18,"gender":"女"},
        {"id":2,"name":"ying","age":18,"gender":"女"},
        {"id":3,"name":"ejie","age":18,"gender":"女"},
        {"id":4,"name":"muyi","age":18,"gender":"男"}

    ],
    "class":[
        {"id":1,"class":"一(1)"},
        {"id":1,"class":"一(2)"},
        {"id":1,"class":"一(3)"},
        {"id":1,"class":"一(4)"}
    ]
}

启动json-server(默认监听端口为3000)

shell 复制代码
json-server --watch students.json

通过url访问

http://localhost:3000

js 复制代码
//返回结果
/student - 4 items
/class - 4 items

http://localhost:3000/student

json 复制代码
//返回结果:
[
{
id: "1",
name: "sally",
age: 18,
gender: "女"
},
{
id: "2",
name: "ying",
age: 18,
gender: "女"
},
{
id: "3",
name: "ejie",
age: 18,
gender: "女"
},
{
id: "4",
name: "muyi",
age: 18,
gender: "男"
}
]

http://localhost:3000/student/1

json 复制代码
//返回结果
{
id: "1",
name: "sally",
age: 18,
gender: "女"
}

8.2使用postman对json-server进行数据操作

8.2.1新增数据
8.2.2删除数据
8.2.3更新数据

8.3增删改查API接口示例

js 复制代码
//api.js
var express = require('express');
var router = express.Router();
const accountModel = require('../db/accountModel');
router.get('/account', function(req, res, next) {
  //获取所有数据api
  accountModel.find().sort({dateTime:-1}).exec().then((accounts)=>{     
    res.json({
      code:'0000',
      msg:'读取成功',
      data:accounts
    })
  }).catch((err)=>{
    res.json({
      code:'1001',
      msg:'读取失败',
      error:err,
      data:null
    })
 
})
})
router.get('/account/:id', function(req, res, next) {
  //获取单条数据api
  let id=req.params.id
  accountModel.find({_id:id}).then((data)=>{     
    res.json({
      code:'0000',
      msg:'读取成功',
      data:data
    })
  }).catch((err)=>{
    res.json({
      code:'1001',
      msg:'读取失败',
      error:err,
      data:null
    })
 
})
})

router.post('/account',function(req,res){
  //插入数据api
  req.body.time=moment(req.body.time).toDate()
  accountModel.create({
    ...req.body

  }).then((data)=>{
    res.json({
      code:'0000',
      msg:'插入数据成功',
      data:data
    })
  }).catch((err)=>{
    console.log(err)
    res.json({
      code:'1002',
      msg:'插入数据失败',
      error:err,
      data:null
    })
  })


})

router.delete('/account/:id',(req,res)=>{
  //删除数据api
  let id=req.params.id
  accountModel.findOneAndDelete({_id:id}).then((data)=>{
    if (!data){
      res.json({
        code:'1002',
        msg:'删除数据失败',
        error:'数据不存在',
        data:null

    })}else{
    res.json({
      code:'0000',
      msg:'删除数据成功',
      data:data
    })
  }


  }).catch((err)=>{
    console.log(err)
    res.json({
      code:'1002',
      msg:'删除数据失败',
      error:err,
      data:null
    })

  })

})

router.patch('/account/:id', function(req, res, next) {
  //更新单条数据api
  let id=req.params.id
  accountModel.updateOne({_id:id},req.body).then((data)=>{  
    //更新成功后重新到数据库读取被更新的数据,并返回给用户   
    accountModel.findOne({_id:id}).then((data)=>{
      res.json({
        code:'0000',
        msg:'更新成功',
        data:data
      })
    }).catch((err)=>{
      res.json({
        code:'1001',
        msg:'读取失败',
        error:err,
        data:null

    })
  })
    
  }).catch((err)=>{
    res.json({
      code:'1001',
      msg:'更新失败',
      error:err,
      data:null
    })
 
})
})
module.exports = router;
相关推荐
aesthetician3 小时前
Node.js v25 重磅发布!革新与飞跃:深入探索 JavaScript 运行时的未来
javascript·node.js·vim
代码搬运媛9 小时前
【架构相关】tsconfig.json 与 tsconfig.node.json、tsconfig.app.json 的关系和作用
node.js·json
EndingCoder9 小时前
WebSocket实时通信:Socket.io
服务器·javascript·网络·websocket·网络协议·node.js
金梦人生14 小时前
🔥Knife4j vs Swagger:Node.js 开发者的API文档革命!
前端·node.js
Ya-Jun15 小时前
快应用TypeError: The ‘compilation‘ argument must be an instance of Compilation错误
node.js·ux·js
亮子AI16 小时前
【npm】npm install 产生软件包冲突怎么办?(详细步骤)
前端·npm·node.js
Q_Q51100828516 小时前
python+uniapp基于微信小程序的心理咨询信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
浪裡遊19 小时前
MUI组件库与主题系统全面指南
开发语言·前端·javascript·vue.js·react.js·前端框架·node.js
梵得儿SHI21 小时前
Vue 开发环境搭建全指南:从工具准备到项目启动
前端·javascript·vue.js·node.js·pnpm·vue开发环境·nvm版本管理
郏国上1 天前
图片上传阿里云
阿里云·node.js·云计算