I install this package http-proxy-middleware to consume multiple end points in react 17.0
I placed setup/setupProxy.js file src/
Problem is multiple proxies called locally working fine, but the problem is when I deployed the build on the server or run the build locally it gives 404 not found
Info
There is no .env file in my project is it OK?
After adding homepage: "." it gives enable javascript error
I try multiple browsers but still got the error when I run the build locally
setupProxy.js
module.exports = function (app) {
app.use(
createProxyMiddleware('/api', {
target: 'https://server1.com', // API endpoint 1
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
headers: {
Connection: 'keep-alive',
},
})
);
app.use(
createProxyMiddleware('/control', {
target: 'https://server2.com', // API endpoint 2
changeOrigin: true,
pathRewrite: {
'^/control': '',
},
headers: {
Connection: 'keep-alive',
},
})
);
};
package.json
{
"name": ".....",
"version": "0.1.0",
"private": true,
"homepage": ".",
"dependencies": {
"#material-ui/core": "^4.12.3",
"#mui/material": "^5.0.6",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"antd": "^4.16.13",
"axios": "^0.24.0",
"bootstrap": "5.1.3",
"http-proxy-middleware": "^2.0.4",
"lodash": "^4.17.21",
"material-ui-color": "^1.2.0",
"material-ui-popup-state": "^2.0.0",
"moment": "^2.29.1",
"prop-types": "^15.7.2",
"rc-color-picker": "^1.2.6",
"react": "^17.0.2",
"react-bootstrap": "^2.0.0",
"react-bootstrap-range-slider": "^3.0.3",
"react-bootstrap-timezone-picker": "^2.0.1",
"react-dom": "^17.0.2",
"react-icons": "^4.3.1",
"react-router-dom": "^5.3.0",
"react-scripts": "4.0.3",
"react-switch": "^6.0.0",
"react-time-picker": "^4.4.4",
"react-toastify": "^8.1.0",
"rgb-hex": "^4.0.0",
"web-vitals": "^1.0.1"
},
"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"
]
}
}
login
Using fetch package
await fetch('/api/v4/auth/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
withCredentials: true,
Related
package.json
{
"name": "portfolio1",
"version": "0.1.0",
"private": true,
"proxy": "http://65.2.97.16:3000",
"dependencies": {
"#emotion/react": "^11.10.0",
"#emotion/styled": "^11.10.0",
"#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",
"#heroicons/react": "^1.0.6",
"#mui/material": "^5.10.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"overlay-navbar": "^1.2.3",
"react": "^18.1.0",
"react-alert": "^7.0.3",
"react-alert-template-basic": "^1.0.2",
"react-dom": "^18.1.0",
"react-helmet": "^6.1.0",
"react-icons": "^4.4.0",
"react-js-pagination": "^3.0.3",
"react-line-awesome": "^1.3.2",
"react-rating-stars-component": "^2.2.0",
"react-redux": "^8.0.2",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-to-print": "^2.14.7",
"redux": "^4.2.0",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "nodemon App.js"
},
"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"
]
},
"devDependencies": {
"autoprefixer": "^10.4.8",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8"
},
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
"main": "postcss.config.js",
"keywords": [],
"author": "",
"license": "ISC"
}
contactAction.js
import {
CONTACT_REQUEST,
CONTACT_SUCCESS,
CONTACT_FAIL,
CLEAR_ERRORS,
} from "../constants/contactConstant";
import axios from "axios";
///COntact US SEction
export const contactUs = (userData) => async (dispatch) => {
try {
dispatch({ type: CONTACT_REQUEST });
const config = { headers: { "Content-Type": "application/json" } };
const { data } = await axios.post(`/api/contact`, userData, config);
dispatch({ type: CONTACT_SUCCESS, payload: data.contact });
} catch (error) {
dispatch({
type: CONTACT_FAIL,
payload: error.response.data.message,
});
}
};
// Clearing Errors
export const clearErrors = () => async (dispatch) => {
dispatch({ type: CLEAR_ERRORS });
};
our backend is working properly on aws but when we try to fetch data or post data on our react website which is also hosted on aws it showing error that xhr.js:220 "POST https://www.apieromedica.com/api/contact 404 (Not Found)" but when we try it on our system on localhost everthing work please help me.
After performing npm run build on my app, I am not able to make requests to my PHP backend at port 8000. It works when I am running it via npm start, but it suddenly does not work when I serve the build folder using serve -s build.
I have confirmed that my backend server is working.
Here is my package.json file:
{
"name": "name",
"version": "0.1.0",
"proxy": "http://localhost:8000/index.php",
"homepage": ".",
"dependencies": {
"#material-ui/core": "^4.12.4",
"#material-ui/icons": "^4.11.3",
"#material-ui/lab": "^4.0.0-alpha.61",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"http-proxy-middleware": "^2.0.6",
"jquery": "^3.6.0",
"material-ui-icons": "^1.0.0-beta.36",
"qs": "^6.10.3",
"react": "^18.1.0",
"react-bootstrap": "^2.3.1",
"react-dom": "^18.1.0",
"react-hooks-global-state": "^1.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-select": "^3.2.0",
"styled-components": "^5.3.5",
"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"
]
}
}
Reverse proxy
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = (app) => {
app.use(
createProxyMiddleware("/test", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/login", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/getAllRooms", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/updateRoom", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/AddNewRoom", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/logout", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
app.use(
createProxyMiddleware("/register", {
target: "http://localhost:8000/index.php",
changeOrigin: true,
})
);
};
Again it works on npm start, but not on npm run build.
I am getting a POST http://localhost:3000/handledata 500 (Internal Server Error)
when I am trying to post some data that I need to handle on the backend , below is the code for my front and backend anything missing ? or is there something wrong ? why would the post post to port 3000 instead of 5000 ? in my package.json I am calling a proxy is this even related to the error ?
// Fetch front end call
fetch('/handledata', {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({ a: 1, b: 2 }),
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
// Express server
import express from 'express';
import bodyParser from 'body-parser';
//import cors from 'cors'
import CryptoJs from 'crypto-js';
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
const PORT = 5000; //process.env.PORT || 5000;
const router = express.Router();
// Test server
app.get('/test', (request: any, response: any) => {
response.send('working......................');
});
// Post data to handle
app.post('/handledata', (req: any, res: any) => {
console.log(req.body);
const handeleddata = // handledata logic could be anything !
res.json(handeleddata);
});
app.listen(PORT, console.log(`Server Started on Port ${PORT}`)!);
frontend package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#emotion/react": "^11.7.1",
"#emotion/styled": "^11.6.0",
"#material-ui/core": "^4.12.3",
"#material-ui/icons": "^4.11.2",
"#material-ui/lab": "^4.0.0-alpha.60",
"#paypal/react-paypal-js": "^7.5.1",
"#testing-library/jest-dom": "^5.16.2",
"#testing-library/react": "^12.1.3",
"#testing-library/user-event": "^13.5.0",
"#types/html2canvas": "^1.0.0",
"#types/jest": "^27.4.1",
"#types/jspdf": "^2.0.0",
"#types/node": "^16.11.26",
"#types/react": "^17.0.39",
"#types/react-dom": "^17.0.13",
"#types/react-router-dom": "^5.3.2",
"#types/uuid": "^8.3.3",
"axios": "^0.26.0",
"firebase": "^9.6.7",
"firestore-size": "^2.0.7",
"html2canvas": "^1.3.3",
"jspdf": "^2.4.0",
"notistack": "^1.0.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-ga": "^3.3.0",
"react-router-dom": "^5.3.0",
"react-scripts": "5.0.0",
"typescript": "^4.4.4",
"uuid": "^8.3.2",
"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"
]
},
"proxy": "http://localhost:5000"
}
Backend package.json
{
"name": "app",
"version": "1.0.0",
"engines": {
"node": "14.17.4",
"npm": "6.14.14"
},
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/server.js",
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"nodemon src/index.ts\" \"npm run client\"",
"build": "tsc -p .",
"heroku-postbuild": "npm run build && NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "me",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"client": "file:client",
"config": "^3.3.6",
"crypto-js": "^4.1.1",
"express": "^4.17.1",
"express-validator": "^6.12.1",
"firebase": "^9.6.7",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.0.6",
"npm": "^8.1.4",
"ts-node": "^10.5.0",
"typescript": "^4.4.4"
},
"devDependencies": {
"#types/crypto-js": "^4.1.1",
"#types/express": "^4.17.13",
"#types/node": "^17.0.18",
"concurrently": "^6.2.1",
"nodemon": "^2.0.12"
}
}
You need to modify your fetch request API url to give the complete hostname:
fetch('http://localhost:5000/handledata')
also you need to setup the cors in your backend
app.use(cors());
I want to deploy my react website to Heroku and I am following this tutorial : https://medium.com/jeremy-gottfrieds-tech-blog/tutorial-how-to-deploy-a-production-react-app-to-heroku-c4831dfcfa08.
I've made the express server and changed my package.json as shown in the tut but when I run yarn startand navigate to localhost:8080. I can see my page but in terminal I see this
C:\Users\ivanr\Documents\GitHub\bees>yarn start
yarn run v1.22.4
$ node server.js
events.js:200
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open 'C:\Users\ivanr\Documents\GitHub\bees\build\favicon.ico'Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:120:12
at FSReqCallback.oncomplete (fs.js:146:23) {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\ivanr\\Documents\\GitHub\\bees\\build\\favicon.ico'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
server.js
const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
return res.send('pong');
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
package.json
{
"name": "business-scheduler",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"apollo-boost": "^0.4.7",
"apollo-link-context": "^1.0.20",
"apollo-link-ws": "^1.0.20",
"bootstrap": "^4.5.0",
"concurrently": "^5.2.0",
"express": "^4.17.1",
"express-favicon": "^2.0.1",
"file-loader": "^6.0.0",
"graphql": "^15.0.0",
"moment": "^2.26.0",
"moment-range": "^4.0.2",
"node-sass": "^4.14.0",
"path": "^0.12.7",
"react": "^16.13.1",
"react-apollo": "^3.1.5",
"react-bootstrap": "^1.0.1",
"react-calendar": "^3.0.1",
"react-copy-to-clipboard": "^5.0.2",
"react-datepicker": "^3.1.3",
"react-dom": "^16.13.1",
"react-gravatar": "^2.6.3",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"react-timestamp": "^5.1.0",
"react-week-calendar": "^0.1.3",
"subscriptions-transport-ws": "^0.9.16"
},
"scripts": {
"start": "node server.js",
"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"
]
}
}
I did the same thing as the tutorial but it doesn't work. Anyone has an idea as to why it isn't working or has a better way of deploying a React website to Heroku.
I'm at my wits end.
I'm creating a react app that connects to a express backend, as per the tutorial I am following I add the proxy to the package.json of the react app:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"moment": "^2.24.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-moment": "^0.9.7",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
"uuid": "^7.0.3",
},
"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"
]
},
"proxy": "http://localhost:5000"
}
However when I run a request with axios:
const config = {
headers: {
'Content-Type': 'application/json',
},
};
const body = JSON.stringify({ name, email, password });
const res = await axios.post('/api/users', body, config);
instead of calling localhost:5000/api/users it calls localhost:3000/api/users
I have even tried creating a axios instance:
const axIns = axios.create({
proxy: {
host: '127.0.0.1',
port: 5000,
},
});
Nothing works... please can you tell me what I am doing wrong