Home > other >  Json-server to real URL
Json-server to real URL

Time:08-22

I have a link to my json database (db.json) on Json-server: http://localhost:3000/places

Now i want to upload this json database on real hosting.

The question is how to access an array 'places' in db.json on real hosting url?

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]

CodePudding user response:

JSON server is a program specifically designed to search and edit a JSON file in response to HTTP requests.

It isn't a flat JSON file.

You can't just upload a JSON file to any HTTP server and get the same API presented to you.

The documentation says:

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

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). Then you need to deploy it to your server.

CodePudding user response:

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.

You can not access to an specific part of the JSON file just by URL. you need to provide a page that parses the URL parameters and based on that query, return the proper chunk of the JSON file.

it is possible to do it in different ways, including JS.

input

http://localhost:3000/index.html

output

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

index.html Code

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

CodePudding user response:

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"]));

CodePudding user response:

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. 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.

  • Related