Angular - 使用模拟服务器的假 REST API

位置:首页>文章>详情   分类: Web前端 > 编程技术   阅读(330)   2023-06-26 07:54:31

Learn to 创建模拟 REST 服务器 locally which will 模拟在线 REST API and produce desired JSON responses online. It is very handy to have such REST APIs mocking capability for quick development time.

安装 JSON 服务器

由于我们在node和angular开发环境中,让我们导入json服务器依赖。

$ npm install -g json-server

//Console Output

C:\Users\admin\AppData\Roaming\npm\json-server -> C:\Users\admin\AppData\Roaming\npm\node_modules\json-server\bin\index.js
+ json-server@0.14.0
added 229 packages from 130 contributors in 24.844s

创建模拟 JSON 响应并在线提供服务

  1. 这种方法最好的一点是你不会被迫消耗任何意味着更少数据的虚拟数据。相反,您可以创建要使用的 JSON 数据,并将其放入文件 'db.json' 中并开始提供服务。 <前>{ “雇员”: [ { "id": 1, "name": "Lokesh", "status": "active" }, { "id": 2, "name": "Andy", "status": "Inactive" }, { "id": 3, "name": "Brian", "status": "active" }, { "id": 4, "name": "Charles", "status": "Inactive" } ]
  2. 并使用简单的命令开始在 数据库.json 文件上提供服务。
    $ json-server --watch 'E:\ngexamples\db.json'
    
    \{^_^}/ 你好!
    
    加载 E:\ngexamples\db.json
    完毕
    
    资源
    http://localhost:3000/员工
    
    家
    http://本地主机:3000
    
    随时键入 s + enter 以创建数据库的快照
    正在观看...
  3. Now test the REST API in browser using command 'http://localhost:3000/employees'.

    JSON server running

    请注意,从 JSON 服务器提供的 REST 端点由 数据库.json 中的数据节点确定。它是如此简单和强大。

在评论部分把你的问题告诉我。

快乐学习!!

Ref: json 服务器 github 页面

标签2: Angular Mocking
地址:https://www.cundage.com/article/mock-rest-server.html

相关阅读

Learn to 创建模拟 REST 服务器 locally which will 模拟在线 REST API and produce desired JSON responses online...
In this angular 2组件教程, learn what is a component in 有角度的, how to create angular components, angul...
1. 什么是 Angular 模板和视图 模板 是一个 HTML 片段,它告诉 Angular 如何在角度应用程序中渲染组件。 该模板直接与组件相关联,定义了该组件的护理。 2. Angular...
学习在本地工作站中设置 Angular、Node 和 TypeScript 的开发环境,以开发下一代 UI 应用程序。 安装节点 Goto 节点下载页面 and download the ins...
JavaScript 是一个单线程应用程序,如果我们继续为所有任务使用该线程,用户最终将等待其他任务,这将妨碍用户体验。为了解决这个问题,我们有了异步编程。 TypeScript 支持回调函数,...
在 Node 上工作时,您可能会下载各种 npm 包。有时下载过程只是挂起而不会继续。您可能还会看到与无法访问的 registry.npmjs.org URL 相关的错误。 1.错误日志 通常错...
Jest 教程展示了如何使用 Jest 框架在 JavaScript 应用程序中执行单元测试。 有 有 专注于简单性的 JavaScript 休息框架。 Jest 是由 Facebook 工程师...