【Node.js】json-server

概述

json-server 主要用于快速开启一个后端服务,并手动设置模拟接口数据。

以下来源于 json-server - npm (npmjs.com)

安装

npm install json-server

用法

创建文件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"
  }
}

将其传递给 JSON 服务器 CLI

$ npx json-server db.json

获取 REST API

$ curl http://localhost:3000/posts/1
{
  "id": "1",
  "title": "a title"
}

运行选项列表json-server --help

基于示例,将获得以下路由:db.json

GET    /posts
GET    /posts/:id
POST   /posts
PUT    /posts/:id
PATCH  /posts/:id
DELETE /posts/:id

# Same for comments
GET   /profile
PUT   /profile
PATCH /profile

相关推荐

  1. Linux server

    2024-04-23 07:48:06       30 阅读
  2. sql server

    2024-04-23 07:48:06       35 阅读
  3. APM Server监控

    2024-04-23 07:48:06       65 阅读
  4. SQL Server事务(Transaction)

    2024-04-23 07:48:06       55 阅读
  5. Linux Server Quick Command

    2024-04-23 07:48:06       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 07:48:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 07:48:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 07:48:06       82 阅读
  4. Python语言-面向对象

    2024-04-23 07:48:06       91 阅读

热门阅读

  1. Ruby中Rack中间件的作用是什么?如何应用?

    2024-04-23 07:48:06       59 阅读
  2. Kubernetes 核心技术组件Scheduler解析

    2024-04-23 07:48:06       122 阅读
  3. I fumo 星(STL,数学)

    2024-04-23 07:48:06       40 阅读
  4. Nginx四层负载均衡

    2024-04-23 07:48:06       118 阅读
  5. CSS3 transition过渡:打造流畅动画效果的全面指南

    2024-04-23 07:48:06       105 阅读
  6. 天星金融消保课堂开讲,金融健康意识再提升

    2024-04-23 07:48:06       39 阅读
  7. 说说redis的集群的原理吧

    2024-04-23 07:48:06       38 阅读
  8. redis 无占用 两种方式 清除大批量数据 lua脚本

    2024-04-23 07:48:06       37 阅读
  9. gitlab上传新创建的工程项目

    2024-04-23 07:48:06       210 阅读
  10. MySQL-数据目录

    2024-04-23 07:48:06       137 阅读
  11. 2007. 从双倍数组中还原原数组

    2024-04-23 07:48:06       38 阅读
  12. 微服务(学习)

    2024-04-23 07:48:06       41 阅读
  13. 介绍下volatile关键字

    2024-04-23 07:48:06       37 阅读