SergioZhidkov
SergioZhidkov

Reputation: 381

Operating response status code in react application

I'm a new in react.js/express.js and need some help.

I have two servers: http://192.168.10.10:5000 - backend, handling by express.js; http://192.168.10.10:5001 - frontend client, react;

It perfectly works.

In react I made SPA application, and i'm trying to make simple pageNotFound component.

Its easy to do with construction:

<Switch>
   <Route exact path="/test1" component={Component1}/>
   <Route exact path="/test2" component {Component2}/>
   <Route exact path="/test3" component={Component3}/>
   <Route component={Page404}/>
</Switch>

It works, when my url didnt matched: /test999 shows me: Sorry, not found. But HTTP status code didnt change - it 200 OK and this is normal, cause react js cant do that. But expressjs can.

In react package.json file:

"proxy": "http://192.168.10.10:5000" - that means, that it will proxy requests to backend. And it also works, when i wrote something like:

axios.get('/api/getsomething')...

Backend also handling 404, when he cant find routes that matched to my request.

So my question is: why http://192.168.10.10:5001/test999 return status 200? By my logic the request must be proxied to the backend and return 404. Maybe i do something wrong?

And how can I solve it?

Thanks a lot.

server.js

const express = require('express');
const app = express();
const port = 5000;

app.get('/api/getsomething',(req,res) => {
    const something= [
        {id:1, message:'Hello'},
    ];

    res.json(something);
});

app.use(function(req, res, next) {
    res.status(404).send('Sorry, not found');
});

app.listen(port, () => console.log('server started'));

Upvotes: 1

Views: 653

Answers (1)

tom
tom

Reputation: 10601

You can give the route a status:

<Route component={Page404} status={404} />

Upvotes: 1

Related Questions