简体   繁体   English

Json-server 到真正的 URL

[英]Json-server to real URL

I have a link to my json database (db.json) on Json-server: http://localhost:3000/places我在 Json 服务器上有一个指向我的 json 数据库(db.json)的链接:http://localhost:3000/places

Now i want to upload this json database on real hosting.现在我想在真实主机上上传这个 json 数据库。

The question is how to access an array 'places' in db.json on real hosting url?问题是如何在真实主机 url 上访问 db.json 中的数组“位置”?

Something like this is not working (certainly it is not working but just to better clarify the question):像这样的东西不起作用(当然它不起作用,只是为了更好地澄清问题):

https://domaindomain.org/db.json[places] or https://domaindomain.org/db.json[0] https://domaindomain.org/db.json[places]https://domaindomain.org/db.json[0]

JSON server is a program specifically designed to search and edit a JSON file in response to HTTP requests. JSON 服务器是专门设计用于搜索和编辑 JSON 文件以响应 HTTP 请求的程序。

It isn't a flat JSON file.它不是一个平面 JSON 文件。

You can't just upload a JSON file to any HTTP server and get the same API presented to you.您不能只将 JSON 文件上传到任何 HTTP 服务器并获得相同的 API 呈现给您。

The documentation says:文档说:

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.为需要快速后端进行原型设计和 mocking 的前端开发人员使用 <3 创建。

Before you go to production you need to replace your JSON server prototype with your own server-side code with equivalent functionality (probably working with a real database instead of a JSON file).在您将 go 投入生产之前,您需要用您自己的具有等效功能的服务器端代码替换您的 JSON 服务器原型(可能使用真实数据库而不是 Z0ECD11C1D7A287401D148A23BBD7A2 文件)。 Then you need to deploy it to your server.然后你需要将它部署到你的服务器上。

I hope I can help you with some solutions.我希望我能帮助你解决一些问题。 if you are going to use a shared-host;如果您要使用共享主机; you can use JavaScript for parsing JSON.您可以使用 JavaScript 来解析 JSON。

You can not access to an specific part of the JSON file just by URL.您不能仅通过 URL 访问JSON文件的特定部分。 you need to provide a page that parses the URL parameters and based on that query, return the proper chunk of the JSON file.您需要提供一个解析URL参数的页面,并根据该查询返回JSON文件的正确块。

it is possible to do it in different ways, including JS .可以用不同的方式来做,包括JS

input输入

http://localhost:3000/index.html

output output

{
'places':[
{},{}
]
}

index.html Code index.html 代码

fetch('http://localhost:3000/places')
.then(response => response.json())
.then(json => document.body.innerHTML=JSON.stringify(json['places']));

you need to fetch or read the file first.您需要先获取或读取文件。 then you can access properties.然后您可以访问属性。

fetch('http://example.com/movies.json')
  .then((response) => response.json())
  .then((data) => console.log(data["places"]));

If you are on a VPS you can install node on your machine and start your db server, maybe yo get yourip:3000, then in your DNS record add domain.com to yourip:3000.如果你在 VPS 上,你可以在你的机器上安装节点并启动你的数据库服务器,也许你得到 yourip:3000,然后在你的 DNS 记录中添加 domain.com 到 yourip:3000。 In case you the node closed when you logged out, please do research about supervisor.如果您在退出时节点关闭,请研究主管。 Supervisor in a simple term is a service that let your command alive forever.简单来说,Supervisor 是一项让你的命令永远存在的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM