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()
相关推荐
ID_1800790547314 小时前
企业级实战:淘宝商品详情 API简要说明,json数据返回参考
json
学习3人组18 小时前
业务主表+JSON自定义字段
java·spring boot·json
Car121 天前
在vscode中添加一个tasks.json实现 rt thread的scons编译功能
vscode·json·build·scons
学习3人组1 天前
基于 主表 + JSON 自定义字段的条件查询
json
XMYX-01 天前
28 - Go JSON 数据操作
开发语言·golang·json
次次皮2 天前
代理启动前端dist包
java·前端·vue
展示猪肝2 天前
Vue2 + FastAPI + Dify 实现 AI 医疗预检分诊助手:从问诊追问到医生审核闭环
人工智能·vue·fastapi·dify
Promise微笑2 天前
Geo专家于磊:Json-LD优化实战SOP与双核四驱体系
大数据·人工智能·重构·json
专注VB编程开发20年2 天前
json和python元组,列表,字典对比
开发语言·python·json·php
何忆清风2 天前
Easy Agent Pilot - Rust实现的开源桌面Agent软件
ai·rust·vue·agent·tauri·开发工具