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;
相关推荐
嘵奇30 分钟前
Node.js二:第一个Node.js应用
node.js
m0_748252389 小时前
Node.js HTTP模块详解:创建服务器、响应请求与客户端请求
服务器·http·node.js
m0_748234081 天前
Node.js使用教程
node.js·编辑器·vim
雲小妖1 天前
PowerShell 执行策略:fnm管理软件安装nodejs无法运行npm,错误信息:about_Execution_Policies
前端·npm·node.js
qq_530245191 天前
nvm下载node版本npm下载失败
前端·npm·node.js·nvm
Winson℡1 天前
在使用 npm link 进行本地 npm 包调试时,是否需要删除项目中已安装的依赖包取决于你的调试场景和依赖管理方式
前端·npm·node.js
糖果店的幽灵2 天前
Ubuntu 安装 Node.js 20.x
linux·ubuntu·node.js
AlgorithmAce2 天前
解决npm/yarn等包管理工具在vscode中使用出现系统禁止运行脚本的情况
前端·npm·node.js
超级无敌谢大脚2 天前
前端包管理工具进化论:npm vs yarn vs pnpm 深度对比
前端·npm·node.js
i建模2 天前
Windows前端开发IDE选型全攻略
前端·ide·windows·node.js·编辑器·visual studio code