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()
相关推荐
Geoking.8 小时前
JSON vs JSONL:从数据格式到 AI Agent 的工程实践
人工智能·深度学习·json
ID_1800790547312 小时前
京东商品详情API能力解析与标准化应用方案(含JSON返回示例)
json
jieyucx14 小时前
Nuxt4阶段一:环境搭建与第一个 Nuxt 4 项目
vue·nuxt·全栈·ssr
小堂子这厢有礼了1 天前
Chet.Admin 模块详解②:用户管理与个人中心
vue·rbac·后台管理系统·用户权限·.net10·vbenadmin
sugar__salt1 天前
Vue.js 前置知识:ES6+ 核心特性完全指南
前端·javascript·vue.js·vue·es6
灯澜忆梦1 天前
Go 语言 _JSON---序列化与反序列化
开发语言·golang·json
sugar__salt2 天前
Vue3 基础知识点整理 —— 从入门到响应式核心完全指南
vue.js·笔记·vue·vue3
梦幻通灵2 天前
Notepad++格式化Json两种方案【持续更新】
android·json·notepad++
解局易否结局2 天前
鸿蒙原生开发实战|Native JSON 编解码与高性能数据序列化
华为·json·harmonyos
代码的小搬运工3 天前
网络请求(NSURL、NSURLRequest、NSURLSessionDataTask、协议回调与 JSON 数据的基本流向)
网络·数据库·json