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()
相关推荐
狼性书生7 小时前
uniapp实现的Tab 选项卡组件模板
前端·uni-app·vue·组件·插件
水月wwww14 小时前
vue学习之组件与标签
前端·javascript·vue.js·学习·vue
星光一影14 小时前
基于SpringBoot智慧社区系统/乡村振兴系统/大数据与人工智能平台
大数据·spring boot·后端·mysql·elasticsearch·vue
Jonathan Star16 小时前
Vue JSON结构编辑器组件设计与实现解析
vue.js·编辑器·json
IT教程资源C1 天前
(N_151)基于微信小程序校园学生活动管理平台
mysql·vue·前后端分离·校园活动小程序·springboot校园活动
工业甲酰苯胺2 天前
实现 json path 来评估函数式解析器的损耗
java·前端·json
AI3D_WebEngineer2 天前
企业级业务平台项目设计、架构、业务全解之组件库篇
前端·javascript·vue
木子李BLOG2 天前
vue.js设计与实现(待续)
vue
optimistic_chen2 天前
【Java EE进阶 --- SpringBoot】统一功能处理
java·spring boot·java-ee·json·统一功能处理
悟能不能悟3 天前
reason: unable to verify the first certificate 如何处理
vue