0

So I'm trying to connect my React App to an express route - so far for my server, I have

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

app.use('/test', require('./client/src/App'));

app.listen(port, () => console.log(`Server started on port ${port}`));

I'd like to be able to go to localhost:5000/test and have my create-react-app (./client/src/App) show up.

I have my server running on 5000 and React running on 3000.

I realize React has its own routing, but I was wondering if it was possible this way.

3 Answers 3

4

This wouldn't work that way. Express.js is unaware of the thing like react. You need to handle routing of react application using react-router not this way.

Express.js is only responsible for creating endpoints that is in context of express not react.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to use an express route like that, you might only be able to do it with the react build instead of the react sources.

Otherwise, if you want the react development environment (Let's say running on localhost:3000), you should be able to use a reverse proxy on express or nginx.

Comments

2

Express is for server side app and react is for client side app, Both are different app, so you can't render the react page with express,The only way to do that is SSR (Server side rendering), without that is not possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.