Grambam
Grambam

Reputation: 554

Use express route to show React page

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.

Upvotes: 0

Views: 202

Answers (3)

Ashish
Ashish

Reputation: 410

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.

Upvotes: 2

oktapodia
oktapodia

Reputation: 1748

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.

Upvotes: 2

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 4748

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.

Upvotes: 4

Related Questions