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;
相关推荐
垣宇8 小时前
Vite 和 Webpack 的区别和选择
前端·webpack·node.js
爱吃南瓜的北瓜9 小时前
npm install 卡在“sill idealTree buildDeps“
前端·npm·node.js
翻滚吧键盘9 小时前
npm使用了代理,但是代理软件已经关闭导致创建失败
前端·npm·node.js
浪九天10 小时前
node.js的版本管理
node.js
浪九天11 小时前
node.js的常用指令
node.js
浪九天14 小时前
Vue 不同大版本与 Node.js 版本匹配的详细参数
前端·vue.js·node.js
小纯洁w1 天前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
熬夜不洗澡1 天前
Node.js中不支持require和import两种导入模块的混用
node.js
bubusa~>_<1 天前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js
天下皆白_唯我独黑1 天前
npm 安装扩展遇到证书失效解决方案
前端·npm·node.js