unable to hit my nodejs api while deploying project on heroku server - node.js

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"
]
}
}

Related

Is there something I can put in my React package.json to update the url of my backend fetching?

I'm currently in the process of deploying a MERN stack app. I'm putting the frontend on Netlify and the backend on Adaptable. I need to change the fetching on the frontend so that it goes to the Adaptable site instead of my localhost, but I don't want to manually change every fetch. During development I was using a proxy in the package.json and then just had the simple route in the fetch like '/api/login'. Is there a similar thing I can add to the package.json so that I can quickly change all the fetching rather than go into each fetch manually?
Here is my package.json for my frontend:
{
"proxy": "http://localhost:4000",
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.1.2",
"#fortawesome/free-regular-svg-icons": "^6.1.2",
"#fortawesome/free-solid-svg-icons": "^6.1.2",
"#fortawesome/react-fontawesome": "^0.2.0",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"date-fns": "^2.28.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"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"
]
}
}
Here is an example of one of my fetch requests:
const response = await fetch('/api/checklist', {
headers: {
'Authorization': `Bearer ${user.token}`
}
})
The backend url will be something like this: https://foobar-backend.adaptable.app/

Problem hosting React + Node.js app on heroku

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.

Mern stack app working on heroku local but not working online when deploy

I had my mern stack app on Heroku and previously it is working fine but now it is deploying fine but not working.
All the API are working fine but the frontend is not rendering.
When I visit the homepage it says
/app/client/build/index.html
all the API are working fine as it is
here is my package json for server
"name": "***",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"client-install": "cd client && yarn install ",
"server": "nodemon app",
"start": "node app.js",
"client": "cd client && yarn start ",
"dev": "concurrently \"yarn run server \" \"yarn run client\" ",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false && cd client && npm install && npm build "
},
"repository": {
"type": "***",
"url": "***"
},
"author": "",
"license": "ISC",
"homepage": "***",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.19.0",
"jimp": "^0.6.4",
"morgan": "^1.9.1",
"multer": "^1.4.1"
},
"devDependencies": {
"concurrently": "^4.1.1"
}
}
And here is my package json for client
{
"name": "***",
"version": "0.1.0",
"private": ***,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.19",
"#fortawesome/free-brands-svg-icons": "^5.9.0",
"#fortawesome/free-regular-svg-icons": "^5.9.0",
"#fortawesome/free-solid-svg-icons": "^5.9.0",
"#fortawesome/react-fontawesome": "^0.1.4",
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"file-saver": "^2.0.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"react-toastify": "^5.3.2",
"reactstrap": "^8.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"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": {
"node-sass": "^4.12.0"
}
}
In both, I had dev dependencies and I switched from yarn to npm in my Heroku post-build.
here is my routing file in app.js
app.use(express.static(path.join(__dirname, "client", "build")));
app.use("/*", (req, res) => {
res.send(path.join(__dirname, "client", "build", "index.html"));
});
If I had done any mistake then please point.
Hi I was having the same problem, it's a mongodb Network access issue. I solved it by this: login to MongoDB > select you project > Network Access > ADD IP ADDRESS > click "ALLOW ACCESS FROM ANYWHERE"
use
app.use('*', express.static(path.join(__dirname, "client", "build")))
instead of
app.use("/*", (req, res) => {
res.send(path.join(__dirname, "client", "build", "index.html"));
});
I was getting similar error, don't know why, but solved the problem

React app not working in Internet Explorer 11

My react app is not working on Internet explorer 11. It is working fine on edge and chrome.
I have already created a lot of files and i do not think I can try npx create-react-app.( As pointed out in the other answers for similar questions)
I have included this in both my index.js file but it does not work.
import 'react-app-polyfill/ie11';
This is the error I am getting:
I have looked at the links and they ask to resolve a bracket error but there is no issue when I see the code.
This is my package.json file:
{
"name": "insurance_automation",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"config": "^3.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.1",
"mysql2": "^1.6.5",
"node": "^11.15.0",
"nodemailer": "^6.2.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-scripts": "3.0.1",
"sequelize": "^5.8.6",
"universal-cookie": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "nodemon server",
"dev": "concurrently \"npm run server\" \"npm start\" "
},
"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": {
"concurrently": "^4.1.0",
"nodemon": "^1.19.1"
}
}
This worked for me.
Update the development array in package.json
"development": [
"ie 11",
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
Add the below code in index.js in the src folder
// These must be the first lines in src/index.js
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
Then delete the node_modules folder and run
npm install
IE does not support a lot of ES6+ features by default. For more info, refer the create-react-app polyfill docs.

Why does my web-app work fine on localhost (npm run dev) but look like trash when deployed to heroku?

by the way, NOT a duplicate of 19
Javascript features work on localhost but not when deployed to Heroku
I am going to sleep so I can leave my tunneled localhost
up for now which is what I see when I run npm run dev from my machine. And by the way, I feel like this should be some sort of simple package.json fix, so here's my package.json:
{
"name": "hps_prework",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "node app.js",
"start": "node app.js",
"build": "next build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"engines": {
"node": "11.6.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#material-ui/core": "^3.9.2",
"#material-ui/icons": "^3.0.2",
"babel-loader": "^8.0.5",
"jquery": "^3.3.1",
"js-cookie": "^2.2.0",
"koa": "^2.7.0",
"koa-session": "^5.10.1",
"next": "^8.0.3",
"python-shell": "^1.0.7",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-scripts": "^2.1.8",
"webpack": "^4.28.3"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
But here is what my app looks like deployed to heroku:
and here is the console output:
which leads me to believe some resources aren't being loaded for some reason.
What am I missing? repo
Lastly, this is heroku logs --tail ...
2019-03-21T05:40:54.000000+00:00 app[api]: Build succeeded 2019-03-
21T05:41:00.603480+00:00 heroku[web.1]: Starting process with command `npm
start` 2019-03-21T05:41:03.185110+00:00 app[web.1]: 2019-03-
21T05:41:03.185129+00:00 app[web.1]: > hps_prework#1.0.0 start /app 2019-03-
21T05:41:03.185131+00:00 app[web.1]: > node app.js 2019-03-
21T05:41:03.185132+00:00 app[web.1]: 2019-03-21T05:41:03.463015+00:00
app[web.1]: > Ready on http://localhost:25866 2019-03-
21T05:41:04.432538+00:00 heroku[web.1]: State changed from starting to up
2019-03-21T05:41:06.070888+00:00 heroku[router]: at=info method=GET path="/"
host=summaraize.herokuapp.com request_id=96091bf7-f9c1-45f1-b3b9-
5b74ce58826f fwd="104.38.101.109" dyno=web.1 connect=0ms service=517ms
status=200 bytes=3943 protocol=https
Nextjs docs custom server package.json instructions:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
}
Example app using a custom server (express in this case) mars/heroku-nextjs-custom-server-express’s package.json:
"scripts": {
"dev": "node server.js -p $PORT",
  "build": "next build",
  "heroku-postbuild": "next build",
  "start": "node server.js -p $PORT"
},
Your package.json:
You should add the heroku-postbuild script and set NODE_ENV and/or PORT in your start script if needed.

Resources