我正在使用json服务器和axios
标题的结果
link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""
Run Code Online (Sandbox Code Playgroud)
如何从链接中使用/访问这些数据?似乎没有关于如何从github解析或访问它的信息.我尝试link.rels[:last]从github,但它不起作用.
我经常使用npm模块json-server在给定db.json文件的情况下生成伪造的JSON api 。有什么办法可以将其部署到Heroku?本质上,我只是跑步
json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)
并且它运行一台服务器,如果public/目录中有任何内容,该服务器也将处理静态html 。本质上,我认为它只是在运行Node服务器,除了我尝试推送到Heroku之外,但是没有用。为了使Heroku json-server作为服务器运行该模块,我应该执行一个特殊的过程吗?
我刚开始使用json-server并为一件事而苦苦挣扎。我想要嵌套的 URL,例如为了获取用户组织,请求看起来像:
/rest/user/orgs并且将返回用户组织的数组
{
"rest": {
"user": {
"select": {
"org": []
},
"orgs": [{
"id": "5601e1c0-317c-4af8-9731-a1863f677e85",
"name": "DummyOrg"
}],
"logout": {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
我已经安装了 Json 服务器。对此服务器的获取请求正在传递预期的数据,但现在我想向我的请求添加一些搜索查询。但结果还是一样。我不知道我在这里犯了什么错误。
这是我的请求: http://localhost:3000/people?age=22
我也尝试过: http://localhost:3000/people?customer.age=22但结果仍然是所有数据。
那是我的 JSON 文件:
{
"customer": [
{
"id": 1,
"name": "Stefan Winkler",
"phone_number": "017692601589",
"age": "22",
"education": "High School"
},
{
"id": 2,
"name": "Christoph Huber",
"phone_number": "094462649",
"age": "42",
"education": "nothing"
},
{
"id": 3,
"name": "Michael Unholer",
"phone_number": "093862649",
"age": "12",
"education": "Realschule"
}
]
}
Run Code Online (Sandbox Code Playgroud) 我有一个JSON文件夹,我想用它来创建一个简单的API.
这是我的文件夹结构的简化版本:
/clients.json
/clients/1/client.json
/clients/2/client.json
...
我的/clients.json文件看起来像这样:
[
{
"id": 1,
"name": "Jon Parker"
},
{
"id": 2,
"name": "Gareth Edwards"
},
...
]
Run Code Online (Sandbox Code Playgroud)
我的/clients/1/client.json文件看起来像这样:
[
{
"date": "2014-09-12",
"score": 40,
...
},
{
"date": "2015-02-27",
"score": 75,
...
},
{
"date": "2015-05-10",
"score": 75,
...
},
{
"date": "2016-08-27",
"score": 60,
...
}
]
Run Code Online (Sandbox Code Playgroud)
id from clients.json与相关详细信息所在的文件夹相关.
我在客户端文件夹中有很多JSON文件,而不是在客户端单独加载这些文件,我想使用Node.js创建一个API,这给了我更多的灵活性,即...
返回客户名称和id的列表
/clients
返回客户详细信息
/clients/:id/details
最重要的是,返回所有客户的名称和相关细节
/clients/all/details
我确实开始玩json-server,但它要求你的JSON是一个对象而不是一个数组,不幸的是我坚持使用这个JSON的格式.
感谢任何帮助!
我有假的 api 用于前端测试。
我已经看到 id 需要在 json-server 包中放置或发布您的数据,我的问题是我可以使用不同的密钥而不是 id 来代替 id 吗?
{
id: 1, ---> i want to change this with my custom id
name: 'Test'
}
Run Code Online (Sandbox Code Playgroud) 我已经使用以下配置在 docker 容器中成功运行了json-server一段时间:
码头工人文件:
FROM node:alpine
EXPOSE 3000
COPY deploy/conf/mockBackend.json /opt/mockBackend.json
RUN yarn global add json-server
CMD ["json-server", "/opt/mockBackend.json"]
Run Code Online (Sandbox Code Playgroud)
运行命令:
docker run -d -p 3000:3000 --name mockbackend mockbackend
Run Code Online (Sandbox Code Playgroud)
最近这停止工作。我进入ERR_CONNECTION_REFUSED
浏览器。
默认情况下 json-server 绑定到127.0.0.1,如果这是正确的,这可能会导致此问题。
我不知道为什么它以前工作过,但是将 json 服务器绑定到每个接口json-server -H 0.0.0.0 mockBackend.json使其再次工作。
我现在的问题是json-server手动在图像中以这种方式启动工作正常,但是当我CMD将 Dockerfile的行更改为以下CMD ["json-server", "-H 0.0.0.0", "/opt/mockBackend.json"]错误时:
码头工人日志:
\{^_^}/ hi!
Loading /opt/mockBackend.json
Done
Resources
http:// 0.0.0.0:3000/xxx
http:// 0.0.0.0:3000/yyy
Home
http:// 0.0.0.0:3000
Type s + enter at any …Run Code Online (Sandbox Code Playgroud) 我使用server-json来创建一个假 API,我在 data.json 中有路径“playbook/active”
"playbook/active": [{
"description": "This playbook will install haproxy",
"name": "Testing playbook 3",
"tag": [
"loadbalancer",
"charge"
],
"path": "/etc/ansible/haproxy.yml",
"type": "action",
"id": "4bb107be-9efe-11e9-b3e5-bc5ff4901aa5"
},
{
"path": "google.com",
"description": "This is the playbook before execution",
"tag": [
"webserver",
"tomcat"
],
"id": "faa746b4-9cb7-11e9-9b94-bc5ff4901aa5",
"name": "mysql"
}
]
Run Code Online (Sandbox Code Playgroud)
但我有这个错误
错误:哎呀,在数据库属性“playbook/active”中找到/字符。
我更改为“playbook/active”但同样的错误
我有一个组件,我在其中进行挂载 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) json-server ×10
javascript ×6
json ×4
axios ×1
docker ×1
heroku ×1
hyperlink ×1
hypermedia ×1
ip ×1
node.js ×1
npm ×1
reactjs ×1
server ×1