search

Home  >  Q&A  >  body text

javascript - When does the background actively return 404?

2 questions, using javascript language Node.js express framework development

1. Under normal circumstances, the page requests data. If the data cannot be found, should the return be written using res.send(), or should it be res.sendStatus(404). The latter may not be appropriate, so under what circumstances should the server Will it actively return 404?

2. How does the front end handle this 404? Is it a judgment on err.response.status and links the route to the 404 page? But the browser always reports an error not get

天蓬老师天蓬老师2782 days ago853

reply all(4)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:39:32

    1. 404 means resource not found. If you use RESTful semantics, you should return 404 when the resource is not found. If it is not RESTful semantics, you have to create the semantics yourself. Generally, 200 will be returned for the convenience of front-end processing, and error information will be written in the message body. But this may cause other problems.

    2. It depends on the framework you use. "But the browser always reports the error not get." I don't know what your situation is. 404 is a mistake in the first place.

    If you have your own API, it’s actually easier to say. What I hate most is that some web pages are directly sent to the browser. Obviously they are not found, but they have to return a 200 or even a 302 to jump to a page called "error". Some of them jump to the home page before I even have time to look at them. There is an extra comma in my link. Can I change it manually? There are so many links in a list page. I clicked to see which ones are still there, but all the links with errors jumped to the same page. How do I know which links are invalid?

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:39:32

    Backend processing.
    Send 404 page when all routes are matched

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:39:32

    1. When the backend responds with 404, it can also return the response body. There is nothing wrong with it. If the route cannot be found, it must return 404

    2. The front end must have logic to handle the 404 status of http, and how to handle it varies. But it is definitely inappropriate to jump to the 404 page. Just give me a reminder

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:39:32

    The route did not find the corresponding resource based on your url, so it returned 404 No matching resource found

    reply
    0
  • Cancelreply