json-server 模拟接口数据

json-server - npm> [!IMPORTANT] > Viewing alpha v1 documentation -- usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0). Latest version: 1.0.0-alpha.21, last published: 6 days ago. Start using json-server in your project by running `npm i json-server`. There are 347 other projects in the npm registry using json-server.https://www.npmjs.com/package/json-server?activeTab=readme局部安装:

bash 复制代码
npm i json-server

全局安装

bash 复制代码
npm install -g json-server

在文件根路径创建文件夹 data/db.json

定义你想要的json内容

javascript 复制代码
{
    "posts":[
        {
            "title":"json-server 安装方法",
            "body":" npm i json-server",
            "id":1,
            "tags":[
                "vue3",
                "compostionApi",
                "blog"
            ]
        },
        {
            "title":"json-server 监听方法",
            "body":"json-server --watch data/db.json",
            "id":1,
            "tags":[
                "vue3",
                "compostionApi",
                "blog"
            ]
        },
        {
            "title":"json-server 解决端口冲突",
            "body":"json-server --watch data/db.json --port=3003",
            "id":1,
            "tags":[
                "vue3",
                "compostionApi",
                "blog"
            ]
        }
    ]
}

启动监听

bash 复制代码
json-server --watch data/db.json

终端查看监听的json

页面使用:

javascript 复制代码
// 请求模拟数据
const load=async()=>{
    try{
let data=await axios.get('http://localhost:3000/posts')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
    }catch (err){

    }

}
load()
相关推荐
嵌R式小Z19 小时前
JSON&cJSON
json
麦麦大数据1 天前
F003疫情传染病数据可视化vue+flask+mysql
mysql·flask·vue·大屏·传染病
tan77º2 天前
【项目】分布式Json-RPC框架 - 项目介绍与前置知识准备
linux·网络·分布式·网络协议·tcp/ip·rpc·json
知识分享小能手3 天前
Vue3 学习教程,从入门到精通,Axios 在 Vue 3 中的使用指南(37)
前端·javascript·vue.js·学习·typescript·vue·vue3
Yn3123 天前
在 Python 中使用 json 模块的完整指南
开发语言·python·json
码码哈哈爱分享3 天前
Tauri 框架介绍
css·rust·vue·html
i紸定i4 天前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
尚学教辅学习资料5 天前
Vue3从入门到精通: 4.5 数据持久化与同步策略深度解析
vue·数据持久化
陈涛5756 天前
5个最好用的 JSON 工具推荐:让数据处理变得简单高效
json
IT毕设实战小研6 天前
Java毕业设计选题推荐 |基于SpringBoot的健身爱好线上互动与打卡社交平台系统 互动打卡小程序系统
java·开发语言·vue.js·spring boot·vue·毕业设计·课程设计