Attempting to establish a small JSON Server on window machine with node installed

I am here on my window machine → F:\code\JSProjects\json\server

and node.js is successfully installed on my window machine Now I have to create a JSON server on the local machine at the above path with db.json then what process should I follow on the window.

On Mac they do it something like this:

image

My window prompt →

image

Hi,
The instructions on the project’s homepage are quite concise:

  • Install JSON Server: npm install -g json-server

  • Create a db.json file with some data

    {
      "posts": [
        { "id": 1, "title": "json-server", "author": "typicode" }
      ],
      "comments": [
        { "id": 1, "body": "some comment", "postId": 1 }
      ],
      "profile": { "name": "typicode" }
    }
    
  • Start JSON Server: json-server --watch db.json

  • Now if you go to http://localhost:3000/posts/1, you’ll get: { "id": 1, "title": "json-server", "author": "typicode" }

What part of that are you having trouble with?

2 Likes

Thank you so much. Please bear with me. If we can create any random db.JSON file(with random sample data) then what is the need of creating and depending on a server because JSOn will be accessed by Javascript, which unlike server-side language such as PHP doesn’t require a WAMP or any other local or live servers?

For example, data persistence. The json-server package is just for prototyping and shouldn’t be used in production.

Here, you’re using Node.js as your server-side language.

1 Like

Just to clarify though, are you planning to use the json-server package or are you asking how to set up such a server from scratch?

FWIW, PHP too does have a built-in web server… but as the documentation says, it’s not suited for production either.

1 Like

I will give some time to understand as I am confused because I am new to this, but for now, if you can answer one thing. Once the database db.json is set in a folder w/o node.js affairs wtill JS program still access it or not?

As Node is the server that connects web pages with your local file system, the answer is no.

1 Like

Okies, but I have a question there was no node.js server installed, but I tried XHR and fetch and was able to read “Hello World” from a some.txt file on local machine through JS?

Does that mean .JSON extensions require server requirement, but .txt doesn’t require server?

how? what url did you give XHR?

I tried in fetch.

fetch('simple.txt').then(function (response) {
  return response.text()
   }).then(function (text) {
     console.log(text)
 })

I also tried this one →

if you’re talking about running this code from node, then your server is the nodejs server running on your machine.

if you’re talking about running this code from a browser, then you’re running a web server locally that served the page to you.

You could give me your ip address and I couldn’t read anything from your computer. A server is required to achieve that. You are safe from others being able to do that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.