json-server icon indicating copy to clipboard operation
json-server copied to clipboard

<div id="custom-routes" /> is assigned "undefined" if "--routes" option is not passed.

Open akiko-pusu opened this issue 6 years ago • 1 comments

Hi, first, I'd like to say thank you for this awesome tool!

I'm not sure if this is the expected behavior or not.

Issue and Steps to Reproduce

  • <div id="custom-routes" /> is assigned "undefined" if run son-server without "--routes" option.
  • Browser requests _rules but server returns 404 error in case no routes option (and routes.json) .
    • It seems Fetch API does not handle 404 error by default, so CustomRoutesBlock is not called correctly,

https://github.com/typicode/json-server/blob/6f160773d6d28dfc35c0605bbcb67ab213c9fcce/src/front/index.js#L74

$ json-server -w db.json

GET /db 304 3.314 ms - -
GET /__rules 404 8.448 ms - 2

Versions

$ json-server -v
0.15.0

$ node -v
v12.4.0

Screenshots

Expected

expected-rresult


Actual

undefined-message


Workaround

I'm not sure this is the right way, but I can catch the 404 error like this. I hope this would be some of help.

window.fetch('__rules')
  .then(response => {
    if (!response.ok) {
      throw Error(response.statusText); 
    } else { 
      return response.ok; 
    }
  })
  .then(response => response.json())
  .then(customRoutes => document.getElementById('custom-routes').innerHTML = CustomRoutesBlock({
    customRoutes}))
  .catch(error => console.log(error));

akiko-pusu avatar Jun 25 '19 14:06 akiko-pusu

came here to open the same issue, seems like this is still a problem

hkennyv avatar Nov 25 '21 07:11 hkennyv