简体   繁体   中英

How to return 404 status code in ReactJS?

How can I return 404 status code in React (with react-router-dom)?

<Route component={NotFoundPage} path="*" />

The answers I got can return a 404 page, but not the http status code. Is there any way around that?

Your Js or any client side service doesnt return response with code. You can get 404 status code from server. In front end there is no way to send status code. Status code is sent by server depending on the request sent by Client side app.

React runs on the client. Under normal circumstances, you can't generate a 404 error response because there is no request or response at all.

If you were using some server-side renderer (such as Next.js) then it would likely provide a method for generating a 404 error, but it would be specific to that SSR framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM