RAHUL
RAHUL

Reputation: 3

Establishing a new react app with existing express webpage

I have a website made from nodejs-express and ejs , it has 5 pages home , events , about ,developer ,gallery , so all these pages are delivered using ejs. Now I need to create a web app using react and have it as another page of the website ,I searched throughout the internet i found no articles mentioning this particular scenario. Please note I dont want my root route to be the react app , when i get a GET request on "/client" I need the react app (client) to load up.

My current senario is expressed in this sand box https://codesandbox.io/s/integrate-react-app-4dwu0?file=/src/index.js

Upvotes: 0

Views: 84

Answers (2)

Sujan Thakare
Sujan Thakare

Reputation: 902

Here is your solution. https://codesandbox.io/s/integrate-react-app-kb1c5?file=/reactAppFiles/index.html

You just need to set static files path and you have to send index.html.

it looks something like this

app.use(express.static(path.join(__dirname, "../reactAppFiles")));

app.get("/client", (req, res) => {
    res.sendFile("index.html", {
    root: path.join(__dirname, "../reactAppFiles")
});

I hope this answers the question.

Upvotes: 0

Vasanth Kumar
Vasanth Kumar

Reputation: 371

Where are you hosting this. You can deploy this at reactapp.yourdomain.com and set up a proxy for /client and route it to this one.

Upvotes: 0

Related Questions