简体   繁体   中英

Page displaying response instead of content

I'm developing a webpage using express for the backend and reactjs for the frontend. All the different views work correctly with their routes in react. But when I create a webpack and run the app on express, there is one route that shows me the response I get instead of the webpage content.

This is how I declare my routes in react.

 <Router>
            <Route exact path="/" component={LoginPage} />
            <Route path="/menu/:accessToken" component={MainPage} /> 
</Router>

And this is the get method for the main page in express

router.get('/menu/:accessToken', function (req, res, next) {
    res.send("OK");
});

Instead of sending a string in response, you need to send html ie index.html file of react app for all urls, which will resolve route based on your routes.

router.get('/*', function (req, res, next) {
    res.send(indexHtmlFile); // set proper content-type etc. This is not exact code to copy paste
});

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