Json-server 模拟后端接口

server/2024/10/18 12:26:57/

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

使用方法:

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

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

{"posts": [{ "id": "1", "title": "a title", "views": 100 },{ "id": "2", "title": "another title", "views": 200 }],"comments": [{ "id": "1", "text": "a comment about post 1", "postId": "1" },{ "id": "2", "text": "another comment about post 1", "postId": "1" }],"profile": {"name": "typicode"}
}

3. 启动 json-server

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

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

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

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

4. 访问接口

GET    http://localhost:3000/posts
GET    http://localhost:3000/posts/:id
POST   http://localhost:3000/posts
PUT    http://localhost:3000/posts/:id
PATCH  http://localhost:3000/posts/:id
DELETE http://localhost:3000/posts/:idGET   http://localhost:3000/profile
PUT   http://localhost:3000/profile
PATCH http://localhost:3000/profile

5. json-server其他参数说明

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

Options:
  -p, --port <port>  Port (default: 3000)
  -h, --host <host>  Host (default: localhost)
  -s, --static <dir> Static files directory (multiple allowed)
  --help             Show this message
  --version          Show version number


http://www.ppmy.cn/server/14461.html

相关文章

碎碎笔记01

1. 多元线性回归 通过现有数据&#xff0c;总结出数据所对应的线性方程的斜率与截距 f ( x 1 , x 2 , . . . , x n ) w 1 x 1 w 2 x 2 . . . w n x n b f(x_1, x_2, ..., x_n) w_1x_1 w_2x_2 ... w_nx_n b f(x1​,x2​,...,xn​)w1​x1​w2​x2​...wn​xn​b w&a…

HTTP如何自动跳转到HTTPS,免费SSL证书如何获取

如今HTTPS已经成为了网站标配&#xff0c;然而&#xff0c;对于一些刚刚起步的网站或是个人博客而言&#xff0c;如何自动跳转到HTTPS&#xff0c;以及免费SSL证书的获取&#xff0c;可能还是一个需要解决的问题。下面就来详细解答这两个问题。 我们需要先了解HTTP与HTTPS的区…

数据中台工具的选型要点_光点科技

数据中台工具扮演着举足轻重的角色。想要全面理解数据中台工具的意义、作用以及应用方式&#xff0c;就必须深入探讨这一概念以及相关实践。 数据中台工具概述 数据中台&#xff0c;是一个支持数据集成、管理、分析和服务的平台&#xff0c;它能够帮助企业统一数据资源&#xf…

【深度学习实战(24)】如何实现“断点续训”?

一、什么是断点续训&#xff1a; 中断的地方&#xff0c;继续训练。与加载预训练权重有什么区别呢&#xff1f;区别在于优化器参数和学习率变了。 二、如何实现“断点续训” 我们需要使用checkpoint方法保存&#xff0c;模型权重&#xff0c;优化器权重&#xff0c;训练轮数…

tomcat到底是干嘛的?

Tomcat 是一个开源的 Java 应用服务器&#xff0c;主要用于托管和运行符合 Java Servlet 和 JavaServer Pages (JSP) 规范的 Java Web 应用程序。它在 Web 开发和部署中扮演着至关重要的角色&#xff0c;具体功能包括&#xff1a; HTTP 请求处理&#xff1a; Tomcat 监听特定端…

如何在three.js中画3D圆弧及半圆弧组成圆

在three.js中画圆弧以及画圆&#xff0c;首先会想到的是ArcCurve&#xff0c;这个曲线API&#xff0c;经过使用发现&#xff0c;他是一个二维平面的&#xff0c;也就是说只在X-Y轴组成的平面可以使用&#xff0c;三维坐标使用的时候不生效&#xff0c;比如说&#xff1a;我期望…

机器学习笔记(一)基本概念

一、浅谈机器学习 1.1 机器学习简介 机器学习目的并不是为了得到最后的运算结果&#xff0c;而是对计算过程进行分析&#xff0c;总结出一套运算的规则。只要数据量足够多&#xff0c;运算规则就越准确。最后可以根据这套规则对没有通过验证的数据进行预算&#xff0c;得到预算…

如何利用美国站群服务器实现有效的SEO优化策略?

如何利用美国站群服务器实现有效的SEO优化策略? 在当今数字化时代&#xff0c;SEO优化对于网站的可见性和吸引力至关重要。站群服务器作为一种有效的SEO策略&#xff0c;可以通过多个相关联的网站在不同服务器上的部署&#xff0c;增强网站的权威性和链接多样性。尤其是在利用…