JSON Server Doesn't Work on Netlify

PLEASE help us help you by writing a good post!

  • we need to know your netlify site name. Example: gifted-antelope-58b104.netlify.app
  • DNS issues? Tell us the custom domain, tell us the error message! We can’t help if we don’t know your domain.
  • Build problems? Link or paste the FULL build log & build settings screenshot

The better the post - the faster the answer.

Hello, I am a beginner of netlify.

here is my netlify site.
There is a problem. When I run this app on local, I should run both react-app(localhost:3000) and json-server api(localhost:3001).
I had to run manually.


Currently, it is normally running just react-app.
json-server in “api” directory is not working
on netlify, I want to run this command
cd api
npm start
“scripts”: {

"start": "json-server -p 3001 -w db.json"

}

Please help me!

You can’t run a live server on Netlify which is what you’re trying to do. Try using Netlify Functions to get JSON responses.

Thank you always…
Could you please explain more detail?
with Netlify functions, can I make json-server work?

No, you can’t get a live server to work in any case. With Netlify Functions you can get responses as JSON. Each function has its own URL. You can call the function, do some processing on the server and return a JSON response. Try here: https://functions-playground.netlify.app/

I’m looking at doing something similar - my build command is json-server /path/to/file.json & yarn build which does work - sort of. When i make a post request the server crashes. Is that what is expected when you say that this can’t work.

Server crashes? How did you get a server to work in the first place?

Just to clarify, Netlify can’t listen for your requests, etc. You can roughly think of it as a cloud storage but only for small HTML, CSS, JS files. They can only be downloaded by the end user. The closest thing to what is being requested here is Netlify Functions as I mentioned above. Read more here: [Support Guide] Can I run a web server, HTTP listener, and/or database at Netlify?

1 Like

yea it was “working” like an hour ago, must have been seeing cache version when there was a static json file. weird. thanks.

Hi! If I may, for novices like me… Netlify functions cannot take any kind of POST, PUT, PATCH, UPDATE, or DELETE call… Because it’s only a FUNCTION :rofl:

It took a couple of days to get this through my thick skull. It can respond to GET… but only with hard-coded data. Once it is built/deployed, it’s just a functional old funky function.

If a function could take in new data (mutate state), it wouldn’t be a function! Because then f(x) wouldn’t always give the same result.

The great thing about Netlify is it gives noooobs like me powers beyond my abilities :blush: