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()
相关推荐
BUG_MeDe7 小时前
json格式字符串解析的简单使用 libjson-c
c语言·开发语言·json
..过云雨18 小时前
【负载均衡oj项目】01. 项目概述及准备工作
linux·c++·html·json·负载均衡
狂龙骄子21 小时前
如何修改ElementUI表单与表格标签label样式
elementui·vue·ruoyi·el-form-item·el-table-column·表单项label·列表头label
好运yoo21 小时前
在package.json中scripts这个配置的命令是什么意思
前端·webpack·json·vite·wepack
Never_Satisfied1 天前
在JavaScript / Node.js中,package.json文件中的依赖项自动选择最新版安装
javascript·node.js·json
book123_0_991 天前
【MySQL】MySQL函数之JSON_EXTRACT
android·mysql·json
未来之窗软件服务1 天前
幽冥大陆(一百12)js打造json硬件管道——东方仙盟筑基期
开发语言·javascript·算法·json·仙盟创梦ide·东方仙盟·东方仙盟算法
符哥20081 天前
基于 OkHttp+Retrofit 实现 JSON / 表单 / XML/Protobuf 数据格式全解析
okhttp·json·retrofit
吃不饱的得可可2 天前
【三方库】jsoncpp
c++·json