• Json-server 模拟后端接口


    json-server,模拟rest接口,自动生成增删改查接口。(官网地址:json-server - npm)

    使用方法:

    1. 安装json-server,npm i json-server -g

    2. 创建json文件,文件中存储list数据,db.json

    1. {
    2. "posts": [
    3. { "id": "1", "title": "a title", "views": 100 },
    4. { "id": "2", "title": "another title", "views": 200 }
    5. ],
    6. "comments": [
    7. { "id": "1", "text": "a comment about post 1", "postId": "1" },
    8. { "id": "2", "text": "another comment about post 1", "postId": "1" }
    9. ],
    10. "profile": {
    11. "name": "typicode"
    12. }
    13. }

    3. 启动 json-server

    终端进入到db.json所在目录,执行 npx json-server db.json 命令启动服务

    注意:如果遇到下面报错,需要升级node版本

    1. PS D:\workspace\learnui\vuex-demo\db> npx json-server index.json
    2. npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
    3. file:///C:/Users/nantian/AppData/Roaming/npm/node_modules/json-server/lib/bin.js:4
    4. import { parseArgs } from 'node:util';
    5. ^^^^^^^^^
    6. SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
    7. at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    8. at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    9. at async Promise.all (index 0)
    10. at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    11. at async loadESM (node:internal/process/esm_loader:88:5)
    12. at async handleMainPromise (node:internal/modules/run_main:61:12)

    服务启动成功,默认端口号3000

    4. 访问接口

    1. GET http://localhost:3000/posts
    2. GET http://localhost:3000/posts/:id
    3. POST http://localhost:3000/posts
    4. PUT http://localhost:3000/posts/:id
    5. PATCH http://localhost:3000/posts/:id
    6. DELETE http://localhost:3000/posts/:id
    7. GET http://localhost:3000/profile
    8. PUT http://localhost:3000/profile
    9. PATCH http://localhost:3000/profile

    5. json-server其他参数说明

    PS D:\workspace\learnui\vuex-demo> json-server --help
    Usage: json-server [options]

    Options:
      -p, --port  Port (default: 3000)
      -h, --host  Host (default: localhost)
      -s, --static

    Static files directory (multiple allowed)
      --help             Show this message
      --version          Show version number

  • 相关阅读:
    波动数列(蓝桥杯)
    Java OA系统日程管理模块
    xterm使用
    C#中的as和is
    js中 slice 用法用法全解析
    X-NAND新架构助力QLC性能飙升
    STM32Cube学习(3)——ADC
    如何使用Python和Numpy实现简单的2D FDTD仿真:详细指南与完整代码示例
    PHP Ueditor 百度富文本编辑器 CDN 远程服务器配置流程
    [docker] volume 补充 & 环境变量 & 参数
  • 原文地址:https://blog.csdn.net/yuyanxinyu/article/details/136401854