我所做的任何事情都不会让 json-server 确认对 db.json 文件的更改。阅读https://github.com/typicode/json-server/issues/177 后。我已经尝试清除缓存并关闭所有浏览器连接等(这在工作流中已经够痛苦了……)但数据和模式仍然保持在上次使用的状态。
REST 调用会更改数据,并且可以看到更改,这工作正常。但是我需要更改架构...
如何使用 db.json 文件中的内容让 json-server 重新启动?
我在不同的服务器上有前端和后端。我需要发出跨域请求。
\n\n我使用的是localhost:4200angular2。我localhost:3000使用json-server。\xd0\x95he 服务器应给出标头:
Access-Control-Allow-Origin: *\nRun Code Online (Sandbox Code Playgroud)\n\n但我不知道如何打开它。
\n我正在使用 json-server 为前端团队伪造 Api。
我们希望有一个功能可以在一次调用中创建多个对象(例如产品)。
在WebApi2或实际的RestApis中,可以像下面这样完成:
POST api/products //For Single Creation
POST api/productCollections //For Multiple Creation
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用 json-server 来实现它。api/products我尝试使用邮递员将以下数据发布到,但它不会拆分数组并单独创建项目。
[
{
"id": "ff00feb6-b1f7-4bb0-b09c-7b88d984625d",
"code": "MM",
"name": "Product 2"
},
{
"id": "1f4492ab-85eb-4b2f-897a-a2a2b69b43a5",
"code": "MK",
"name": "Product 3"
}
]
Run Code Online (Sandbox Code Playgroud)
它将整个数组视为单个项目并附加到现有的 json。
您能否建议我如何在 json-server 中模拟批量插入?或者 Restful Api 应该始终用于单个对象操作?
为了测试客户端的ApiService类,我需要用模拟替换我的真实后端 URL,出于这些目的,我选择了json-server。我设置了一个代理配置来转发所有以http://localhost:4200/v1/apito开头的请求http://localhost:3000:
{
"/api/v1": {
"target": "http://localhost:3000",
"secure": false
}
}
Run Code Online (Sandbox Code Playgroud)
当我发送类似http://localhost:4200/api/v1/users 但不适用于嵌套端点(例如http://localhost:4200/api/v1/auth/token)的请求时,它会起作用。我发现json-server不支持对嵌套对象的请求,因此我更改了data.json,如下所示:
{
"auth_token": {
"access_token":"accesstoken1",
"token_type":"Bearer"
}
}
Run Code Online (Sandbox Code Playgroud)
并为json-server设置paths.json:
{
"/auth/token": "/auth_token"
}
Run Code Online (Sandbox Code Playgroud)
但它仍然无法通过json-server使用paths.json工作:
[0] Other routes
[0] /auth/token -> /auth_token
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有一个组件,我在其中进行挂载 API 调用
import * as React from 'react';
import axios from 'axios';
import './index.scss';
// import {axiosInstance} from '../../mocks/index';
// axios(client)
// axiosInstance(axios);
const FeatureTable = () => {
React.useEffect(() => {
axios.get("http://localhost:8080/users").then(function (response: any) {
console.log(response.data);
});
}, []);
return (
<div className="container">
</div>
)
}
export default FeatureTable;
Run Code Online (Sandbox Code Playgroud)
我已经在不同的文件夹中设置了我的模拟适配器,如下所示
const Axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
import featureTable from './table';
export const axiosInstance = Axios.create();
const mock = new MockAdapter(axiosInstance, { delayResponse: 1000, onNoMatch: "throwException" }); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Angular 4 应用程序。我正在尝试从 JSON 文件中获取数据以构建用户仪表板。
我创建了一个 json 文件并尝试使用 JSON 服务器加载它:$ json-server --watch user.json我收到以下错误:
不支持 user.json 中的“id”(数字)类型。使用对象或对象数组。.
当我删除文件中的“id”字段时,我收到与“name”相同的错误。
我觉得我创建 json 文件的方式有问题。我是 Web 开发的新手,所以不要介意这是一个非常基本的错误。
这是我的 json 文件:
{
"id": 1,
"name":"Kajal Agarwal",
"department":"Information Technology",
"college":"Indian Institute of Engineering Science and Technology, Shibpur",
"subjects":[
{
"title":"Data Structures",
"active":true,
"faculty":"Prasun Ghosal",
"notifications":4,
"color":"#9c27b0"
},
{
"title":"Operating System",
"active":true,
"faculty":"Niharika Singh",
"notifications":0,
"color":"#ffc107"
},
{
"title":"Algorithms",
"active":true,
"faculty":"Debojit Mondal",
"notifications":1,
"color":"#009688"
},
{
"title":"Web Technologies",
"active":true,
"faculty":"Shantanu Saurabh",
"notifications":2,
"color":"#ff5722"
}, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用https://www.npmjs.com/package/json-server作为模拟后端,我能够匹配 get 的 URL,但如何为 POST 调用返回一些模拟响应。
就像创建用户 URL 一样
URL - http://localhost:4000/user
Method - POST
Request Data - {name:"abc", "address":"sample address"}
expected response -
httpStats Code - 200,
Response Data - {"message":"user-created", "user-id":"sample-user-id"}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我还想发送自定义 http 代码,例如 500,423,404,401 等,具体取决于某些数据。
最大的问题是我的代码没有返回任何 POST 响应,它只在 JSON 中插入记录
我目前正在开发一个使用 React(通过create-react-app)和 JSONServer 作为 API 的项目。
我有一个 Git 存储库,结构如下:
|-- client
|-- api
Run Code Online (Sandbox Code Playgroud)
为了在开发环境中工作,我启动这两个文件夹,npm start以便为 React 应用程序提供 http://localhost:3000,为 API 提供 http://localhost:3001。
这样(感谢 Axios)我可以连接到 http://localhost:3001/api 来检索我的内容
import axios from 'axios'
export default axios.create({
baseURL: 'http://localhost:3001/api'
})
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好。
现在我想将我的应用程序和 API 部署到 Heroku,这就是事情变得复杂的地方。
所以这是我的问题,如果能就最好的处理方法提供一些建议将会非常有帮助:
目前,我server.js在客户端文件夹的根目录中有一个文件,以及一个Procfile用于web: node server.js启动构建服务器的文件。然后我使用 Axios 从另一个 Heroku 应用程序(实际上是 …
我正在尝试安装 JSON-Server,但它不起作用!
我使用了以下命令:npm install -g json-server
这是错误消息:
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/json-server
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/json-server'
npm ERR! [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/json-server'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/usr/local/lib/node_modules/json-server'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not …Run Code Online (Sandbox Code Playgroud) 学习reactjs,尝试用json-server模拟服务器
这是我运行服务器的脚本:
"scripts": {
"server": "json-server --watch db.json --port 5000"
},
Run Code Online (Sandbox Code Playgroud)
在终端输出上运行npm run server
:
Debugger listening on ws://127.0.0.1:61616/507f7893-92a9-4526-b8f4-a3e71cfa4c62
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
> my-app@0.1.0 server
> json-server --watch db.json --port 5000
Debugger listening on ws://127.0.0.1:61629/8a2f877f-f4bd-465e-b454-01a63eabb40a
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:5000/tasks
Home
http://localhost:5000
Type s + enter at any time to create a snapshot of the database
Watching...
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试访问 http://localhost:5000/tasks 时,我收到 403 Access to localhost was returned 被拒绝。 …