fseb
fseb

Reputation: 135

Serve multiple reactjs apps - Nodejs + Express + React

I'm trying to serve two separated Reactjs apps using Express.

App1

*Main app: displays different sets of data. Root front-end

*path: "/" (localhost:5000/)

App2

*Secondary App: User Dashboard, specific user-data.

*path: "/app" (localhost:5000/app)

I had a similar issue as: 1: Serving multiple react apps with client-side routing in Express

But that couldn't solve my problem.

Configuration files

App1 package.json

{
  "name": "app1",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.4",
    "@fortawesome/free-solid-svg-icons": "^5.3.1",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "axios": "^0.18.0",
    "classnames": "^2.2.5",
    "jwt-decode": "^2.2.0",
    "moment": "^2.22.0",
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "react-image-gallery": "^0.8.11",
    "react-moment": "^0.7.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.4",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000"
}

App2 package.json:

{
  "name": "app2",
  "version": "2.0.9",
  "private": true,
  "dependencies": {
    "@coreui/coreui": "^2.0.4",
    "@coreui/coreui-plugin-chartjs-custom-tooltips": "^1.2.0",
    "@coreui/icons": "0.3.0",
    "@coreui/react": "^2.0.5",
    "bootstrap": "^4.1.3",
    "chart.js": "^2.7.2",
    "classnames": "^2.2.6",
    "core-js": "^2.5.7",
    "enzyme": "^3.5.0",
    "enzyme-adapter-react-16": "^1.3.1",
    "flag-icon-css": "^3.0.0",
    "font-awesome": "^4.7.0",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-chartjs-2": "^2.7.2",
    "react-dom": "^16.4.2",
    "react-loadable": "^5.5.0",
    "react-router-config": "^1.0.0-beta.4",
    "react-router-dom": "^4.3.1",
    "react-test-renderer": "^16.4.2",
    "reactstrap": "^6.4.0",
    "simple-line-icons": "^2.4.1"
  },
  "devDependencies": {
    "babel-jest": "^23.4.2",
    "node-sass-chokidar": "^1.3.0",
    "npm-run-all": "^4.1.3",
    "react-scripts": "^1.1.5"
  },
  "scripts": {
    "build-css": "node-sass-chokidar --include-path ./node_modules ./src/scss -o ./src/scss",
    "watch-css": "npm run build-css && node-sass-chokidar --include-path ./node_modules ./src/scss -o ./src/scss --watch --recursive",
    "start-js": "react-scripts start",
    "start": "npm-run-all -p watch-css start-js",
    "build-js": "react-scripts build",
    "build": "npm-run-all build-css build-js",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",
  "homepage": "http://localhost:5000/app2",
}

server.js

app.use("/app2", express.static(path.join(__dirname, "app2/build")));
app.get("app2/*", (req, res) => {
  res.sendFile(path.join(__dirname + "/app2/build/index.html"));
});

app.use(express.static(path.join(__dirname, "app1/build")));
app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname + "/app1/build/index.html"));
});

localhost:5000 successfully renders app1, but if I try localhost:5000/app2/ it actually looks for a route=/app2/ within app1, therefore I got a blank page.

PS: I also tried this solution, but again, no good results.

Thank you!

Upvotes: 3

Views: 3098

Answers (1)

Karol Świeca
Karol Świeca

Reputation: 83

By default CRA build will assume that your application is served from root directory. This is the reason you saw a blank page - HTML was served, but requests for js and css failed. (Actually, in your case it succeded - index.html was returned because you have a catch-all at the bottom of your server.js file)

Now this can be parametrized in package.json

If your app will be served at /app2 you need to add

//package.json
{
  //...
  "homepage":"app2"
}

Then your output files will have propper reference. You can check it out

I am attaching link to CRA docs - homepage attribute

Upvotes: 4

Related Questions