Problem hosting React + Node.js app on heroku - node.js

When I work on my project locally (without problem), I do npm start which works on port 3000, also i have to open another terminal and do node server/server.js which works on port 4000. Then I will be able to work with my browser to connect my both frontend and backend. Now I am trying to host this project on heroku but no luck. here are my error:
2020-08-26T11:54:23.905587+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=whatever.herokuapp.com request_id="whatever" fwd="96.20.56.73" dyno=web.1 connect=1ms service=159ms status=503 bytes=0 protocol=https
and package.json:
{
"name": "library",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:4000",
"dependencies": {
"#material-ui/core": "^4.9.9",
"#material-ui/icons": "^4.9.1",
"#material-ui/lab": "^4.0.0-alpha.48",
"#material/react-snackbar": "^0.15.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"chai": "^4.2.0",
"clsx": "^1.1.0",
"cookie-parser": "^1.4.4",
"dotenv": "^8.2.0",
"google-maps-react": "^2.0.2",
"hookrouter": "^1.2.3",
"i18next": "^19.4.1",
"i18next-browser-languagedetector": "^4.0.2",
"i18next-xhr-backend": "^3.2.2",
"immer": "^5.3.6",
"mongo-seeding": "^3.4.1",
"mongodb": "^3.5.3",
"multer": "^1.4.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-facebook-login": "^4.1.1",
"react-google-login": "^5.1.1",
"react-hook-google-maps": "0.0.3",
"react-i18next": "^11.3.4",
"react-redux": "^7.1.3",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"redux": "^4.0.5",
"redux-immer": "^1.0.4",
"sha1": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "mocha --exit",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^3.0.0",
"eslint-plugin-react-redux": "^3.0.3",
"mocha": "^7.2.0",
"supertest": "^4.0.2"
}
}```
is it cause of the proxy? or i have a wrong start script? or something else?

The way I deploy on heroku is:
to the backend package.json scripts I add
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
add the following to my server.js
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (request, response) => {
response.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
After these I just git push heroku master and it gets deployed w/o any issues.

I was using create-react-app + JSX, so i had to open two terminals one for npm start (on port 3000 for react server) and one for node server.js (for backend on port 4000) and these two are connect through the proxy that i had on my package.json. when i have finished with development, all i had to do was:
remove the proxy from package.json.
npm run build, which makes a copy of all my code into the build folder.
app.use("/", express.static("build"));
in my server.js file to make the build folder accessible to app.
app.all("/*", (req, res) => { res.sendFile(__dirname + "/build/index.html"); }); at the end of the server.js to catch all.
hope this saves some of your time.

Related

React app freezes after running npm start . The issue is observed while compilation

After running npm start my react app is either very slow in opening the page on the browser and most of the times my machine freezes and I am forced to reboot my system .
Below is my package.json file .
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.12.3",
"#material-ui/data-grid": "^4.0.0-alpha.37",
"#material-ui/icons": "^4.11.2",
"#material-ui/lab": "^4.0.0-alpha.60",
"#stripe/react-stripe-js": "^1.7.0",
"#stripe/stripe-js": "^1.22.0",
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.24.0",
"country-state-city": "^3.0.1",
"overlay-navbar": "^1.1.1",
"react": "^17.0.2",
"react-alert": "^7.0.3",
"react-alert-template-basic": "^1.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-icons": "^4.2.0",
"react-js-pagination": "^3.0.3",
"react-material-ui-carousel": "^2.3.1",
"react-rating-stars-component": "^2.2.0",
"react-redux": "^7.2.6",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.0",
"redux": "^4.1.2",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"web-vitals": "^2.1.2",
"webfontloader": "^1.6.28"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:4000"
}
node version : 14.17.0
react version : 17.0.2
npm version : 7.20.6
os: Ubuntu 20.04.3
To solve this issue permanently you need to remove the nodejs and npm completely from your computer.
Windows Users can uninstall npm and node and can download the latest one and install. If you have any issue please follow this post to install node and npm.
Linux Users can uninstall and remove nodejs and npm by running the following command.
sudo apt-get remove nodejs
sudo apt-get remove npm
more details are given on this link go and follow it, very helpful
https://aboutreact.com/problem-after-running-npm-start-execution-got-stucked-on-starting-running-packager/

SyntaxError: Unexpected identifier while deploying React application on Docker container

I'm new to ReactJS world and helping one team with deployment of their React application to the Docker container. I have done following tasks.
Created Ubuntu machine in EC2 and installed Docker engine.
Copied the entire React code in one of the folder, added DockerFile and Docker-compose yml file. Team has few other applications to deploy together and that is why I've included docker-compose file. But, right now just deploying one React application.
After doing docker-compose up container is getting created and exiting immediately. I then checked the logs of exited container (shown below).
/usr/src/app/src/index.js:1 import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
I researched on this error and looks like this is because of compiling, missing webpack or something in React application. I'm not sure what to fix in React and where.
Here is package.json file in case if you can help to figure out the problem.
{
"name": "xxxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.30",
"#fortawesome/free-solid-svg-icons": "^5.14.0",
"#fortawesome/react-fontawesome": "^0.1.11",
"#rjsf/core": "^2.3.0",
"#sentry/browser": "^5.23.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"FormData": "^0.10.1",
"append-query": "^2.1.0",
"axios": "^0.19.2",
"babel-loader": "8.1.0",
"bootstrap": "^4.5.2",
"es6-promise": "^4.2.8",
"install": "^0.13.0",
"lodash": "^4.17.20",
"log": "^6.0.0",
"mdbreact": "^4.27.0",
"node-sass": "^4.14.1",
"npm": "^6.14.8",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-tabs": "^2.0.0",
"react-confirm-alert": "^2.6.2",
"react-data-table-component": "^6.11.0",
"react-dom": "^16.13.1",
"react-fontawesome": "^1.7.1",
"react-jsonschema-form": "^1.8.1",
"react-popup": "^0.10.0",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-stepzilla": "^6.0.2",
"react-stepzilla-redux": "0.0.3",
"react-toastify": "^6.0.8",
"react-tooltip": "^4.2.8",
"reactstrap": "^8.5.1",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"styled-components": "5.1.0",
"validate": "^5.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/preset-env": "^7.11.5"
}
}
Here is the content of DockerFile
FROM node:10
# FROM alpine:latest
WORKDIR /usr/src/app
# add node_modules/.bin:$path
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# Install app dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#
EXPOSE 80
CMD npm start
And, docker-compose file looks like below.
version: "3"
services:
web:
build: ./pdf_web_frontend
command: "node src/index.js"
ports:
- "800:80"
Do you see any problem in anything above? TIA.
Note: I have already seen this link SyntaxError: Unexpected identifier importing React (Javascript)
After a lot of research, I could deploy this react app. Here are the issues.
Issue was with the dockerfile and docker-compose file. I added completely new files with different content.
React app was deploying to some miserable port. So, I explicitly added PORT 8080 in package.json file. This is not a clean solution but this worked.
Below are the new files.
package.json
{
"name": "xxxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.30",
"#fortawesome/free-solid-svg-icons": "^5.14.0",
"#fortawesome/react-fontawesome": "^0.1.11",
"#rjsf/core": "^2.3.0",
"#sentry/browser": "^5.23.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"FormData": "^0.10.1",
"append-query": "^2.1.0",
"axios": "^0.19.2",
"babel-loader": "8.1.0",
"bootstrap": "^4.5.2",
"es6-promise": "^4.2.8",
"install": "^0.13.0",
"lodash": "^4.17.20",
"log": "^6.0.0",
"mdbreact": "^4.27.0",
"node-sass": "^4.14.1",
"npm": "^6.14.8",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-tabs": "^2.0.0",
"react-confirm-alert": "^2.6.2",
"react-data-table-component": "^6.11.0",
"react-dom": "^16.13.1",
"react-fontawesome": "^1.7.1",
"react-jsonschema-form": "^1.8.1",
"react-popup": "^0.10.0",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-stepzilla": "^6.0.2",
"react-stepzilla-redux": "0.0.3",
"react-toastify": "^6.0.8",
"react-tooltip": "^4.2.8",
"reactstrap": "^8.5.1",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"styled-components": "5.1.0",
"validate": "^5.1.0"
},
"scripts": {
"start": "PORT=8080 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/preset-env": "^7.11.5"
}
}
Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD ["yarn", "run", "start"]
docker-compose.yml
version: "3"
services:
client:
stdin_open: true
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"

NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

I have a Node/React project in my Webstorm that won't run because of this error. I had to reinstall Windows and start fresh with my development. I got the code back into the IDE, but when I start up the Node server, I am getting the following error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
More context for that error:
[nodemon] starting `babel-node src/node-server/index.js`
internal/validators.js:122
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at validateString (internal/validators.js:122:11)
at Object.join (path.js:375:7)
at Object.<anonymous> (C:\Projects\Production-Orchestrator\src\node-server\/index.js:17:15)
I went to index.js and here is lines 1-17:
// npm run server
import dotenv from 'dotenv';
import express from 'express';
import path from 'path';
import sql from 'mssql';
import cors from 'cors';
import http from 'http';
import { setupWebSocket } from './ws/setupWebSocket.js';
const useWebSockets = true;
dotenv.config();
const dbConfig = {
user: process.env.DB_USER,
password: process.env.DB_PASS,
server: path.join(process.env.DB_SERVER, process.env.DB_HOST),
I am running npm run server to start up my node server.
And here is my package.json if it helps:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"description": "my app",
"homepage": ".",
"repository": {
"type": "git",
"url": "git+https://github.com/xxxxxxx"
},
"license": "UNLICENSED",
"scripts": {
"buildp": "env-cmd -f .env.production react-scripts build",
"buildt": "env-cmd -f .env.test react-scripts build",
"buildw": "webpack --config ./webpack.config.js --mode production",
"eject": "react-scripts eject",
"eslint-check": "eslint --print-config src/components/search/Search.js | eslint-config-prettier-check",
"server": "nodemon --exec babel-node src/node-server/index.js",
"start": "SET REACT_APP_WS_PORT=3001 & react-scripts start",
"startw": "webpack-dev-server --config ./webpack.config.js --mode development --open",
"stylelint": "stylelint **/*.scss",
"test": "react-scripts test",
"ws": "node --experimental-modules src/node-server/websocket.js",
"ws2": "nodemon --exec babel-node src/node-server/websocket.js"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"#babel/preset-react": "^7.8.0",
"#emotion/core": "latest",
"#fortawesome/fontawesome": "^1.1.8",
"#fortawesome/fontawesome-svg-core": "^1.2.27",
"#fortawesome/free-regular-svg-icons": "^5.12.1",
"#fortawesome/free-solid-svg-icons": "^5.12.1",
"#fortawesome/react-fontawesome": "^0.1.8",
"#popperjs/core": "^2.0.6",
"#react-pdf/renderer": "^1.6.8",
"#types/react": "^16.9.19",
"animate.css": "^3.7.2",
"axios": "^0.19.2",
"babel-loader": "^8.0.6",
"bootstrap": "^4.4.1",
"bufferutil": "^4.0.1",
"cors": "^2.8.5",
"dayjs": "^1.8.20",
"device-detector-js": "^2.2.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"file-saver": "^2.0.2",
"javascript-time-ago": "latest",
"jquery": "^3.4.1",
"jsbarcode": "^3.11.0",
"mssql": "^6.1.0",
"popper.js": "^1.16.1",
"print-js": "^1.0.63",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-animations": "^1.0.0",
"react-beautiful-dnd": "^12.2.0",
"react-beforeunload": "^2.2.1",
"react-collapse": "^5.0.1",
"react-confirm-alert": "^2.6.1",
"react-custom-scrollbars": "^4.2.1",
"react-detect-offline": "^2.4.0",
"react-dnd": "^10.0.2",
"react-dnd-html5-backend": "^10.0.2",
"react-dom": "^16.12.0",
"react-modal": "^3.11.1",
"react-notifications-component": "^2.3.0",
"react-popup": "^0.10.0",
"react-radio-group": "^3.0.3",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.3",
"react-select": "^3.0.8",
"react-spinners": "^0.8.0",
"react-spring": "^8.0.27",
"react-time-ago": "^5.0.7",
"react-transition-group": "^4.3.0",
"sort-package-json": "^1.40.0",
"styled-components": "^4.4.1",
"typescript": "^3.7.5",
"utf-8-validate": "^5.0.2",
"ws": "^7.2.5"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/node": "^7.10.5",
"#babel/preset-env": "^7.8.4",
"#welldone-software/why-did-you-render": "^4.2.5",
"css-loader": "^3.4.2",
"dotenv-cli": "^3.2.0",
"env-cmd": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-prettier": "^3.1.2",
"file-loader": "^5.0.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.13.1",
"nodemon": "^2.0.4",
"prettier": "^1.19.1",
"react-hot-loader": "^4.12.19",
"sass-loader": "^8.0.2",
"stylelint": "^13.2.0",
"stylelint-config-rational-order": "^0.1.2",
"stylelint-config-standard": "^19.0.0",
"stylelint-order": "^4.0.0",
"stylelint-scss": "^3.14.2",
"url-loader": "^3.0.0",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.11"
},
"proxy": "http://localhost:3000"
}
OK, I figured out the issue. I thought the error was telling me that path was undefined. When it fact it was saying the variables passed into path.join() were undefined. And that was because I forgot to add in my .env file to the root so it could grab those variables. Whoops!
In my case, the issue was the missing of an env variable. I did not have SECRET. So the SECRET env variable was missing and my test was trying to access it and as a result it was returning undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
For me was the Knex configuration file had a typo.
For testing I had
testing{
...
migrations: {
connection{
directory: './src/db/migrations',
tableName: 'knex_migrations',
}
},
...
}
when I needed
testing{
...
migrations: {
directory: './src/db/migrations',
tableName: 'knex_migrations',
},
...
}
My issue was that I added && after the env-cmd command, so that for my start script I got
"start": "env-cmd -e development && react-scripts start",
instead of
"start": "env-cmd -e development react-scripts start",
When I removed the && everything was fine.
This worked for me.
https://reactgo.com/typeerror-err-invalid-arg-type-react/
It says, that this error occurs when the react-scripts version is old.
So, delete all your current node modules, install the latest react-scripts using: 'npm install react-scripts#latest' and then install all the other dependencies.
I did the exact same thing, it worked.

unable to hit my nodejs api while deploying project on heroku server

I have made a simple project where I am maintaining data of my AWS dynamoDB, on first stage I am fetching all data through db and listing items, It works perfectly when I run project on my local machine but when I deployed this on heroku it crash on hitting my node api shows 404 error here is full error screenshot
I have also test project to run project on static port but won't work.
You can check project https://funcardmaker.herokuapp.com/update
here is my Nodejs code https://gist.github.com/amit0shakya/41a91df6cccb45665fd16b869801922e
This is heroku server log.
2019-07-30T07:59:17.593103+00:00 heroku[router]: at=info method=GET path="/getalldata" host=funcardmaker.herokuapp.com request_id=97923dda-2645-48fd-a0c5-8cf8e69b2955 fwd="139.167.184.88" dyno=web.1 connect=0ms service=5ms status=404 bytes=416 protocol=https
The reason why you can't use getalldata api because you are deploying your react app instead of deploying your express server which serve your production react app. So you need to config your package.json to start express server to serve your production react app. You just need to change start script in package.json to start express instead of react.
{
"name": "cardmaker3",
"version": "0.1.0",
"private": true,
"dependencies": {
"aws-sdk": "^2.464.0",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"dotenv": "^8.0.0",
"ejs": "^2.6.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-redux": "^7.1.0",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start-react": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"start": "node bin/config"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

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!
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

Resources