代理服务器在一种情况下工作,在另一种情况下失败
proxy server working in one case and failed in another case
所以我正在使用 React 和 Node.js 开发电子商务网站。谈到这一点,在登录时代理工作完全正常但是当我向 API 发出请求时它显示错误。我花了 2 天时间解决这个问题,但最终还是带着希望得到答案来到这里。
我的server.js文件
const express = require ( 'express');
const bodyParser = require('body-parser');
const app = express();
const {sequelize} = require('./models')
const cookieParser = require("cookie-parser");
app.use(express.json());
// app.use(express.urlencoded());
// app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
const FarmerRoutes = require('./Routes/Farmer'); // Finished
const AdminRoutes = require('./Routes/Entities'); // Finished
const Ecommerce = require('./Routes/Ecommerce'); // Finished
const upload = require('./Controllers/ImageManagement')
app.use(cookieParser())
app.use(express.static('backend/uploads'))
app.get('/', (req, res) => {
res.send(" CattleTalk Api is active and running...")
})
app.use('/api/ecommerce',Ecommerce);
app.use('/api/admin',AdminRoutes);
app.use('/api/farmers',FarmerRoutes);
app.listen(5002,async()=>{
console.log("The app listening at 5002");
try {
await sequelize.authenticate();
console.log('Database Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
});
我的 package.json 在前端看起来像这样
{
"name": "frontend",
"secure":false,
"version": "0.1.0",
"private": true,
"dependencies": {
"@popperjs/core": "^2.9.1",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"bootstrap": "^5.0.2",
"formik": "^2.2.9",
"js-cookie": "^3.0.1",
"react": "^17.0.1",
"react-bootstrap": "^2.0.0-alpha.2",
"react-bootstrap-sidebar": "^0.0.1",
"react-dom": "^17.0.1",
"react-icons": "^4.2.0",
"react-router": "^5.2.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.1",
"yup": "^0.32.11"
},
"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:5002/"
}
在互联网上探索了几个小时后,我无法得到问题的答案,但调试代码后我发现了问题。实际上,这是 URL 中的一个额外的尾部斜线导致了它的错误行为。
我以前也是这样。
正确的版本是。
额外说明:
所有未从 here should move forward to this link 中找到解决方案并在当前 URL 中找到解决方案的人都在寻找尾部斜线,为了成功代理,您需要消除 URL 末尾的尾部斜线.
所以我正在使用 React 和 Node.js 开发电子商务网站。谈到这一点,在登录时代理工作完全正常但是当我向 API 发出请求时它显示错误。我花了 2 天时间解决这个问题,但最终还是带着希望得到答案来到这里。
我的server.js文件
const express = require ( 'express');
const bodyParser = require('body-parser');
const app = express();
const {sequelize} = require('./models')
const cookieParser = require("cookie-parser");
app.use(express.json());
// app.use(express.urlencoded());
// app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
const FarmerRoutes = require('./Routes/Farmer'); // Finished
const AdminRoutes = require('./Routes/Entities'); // Finished
const Ecommerce = require('./Routes/Ecommerce'); // Finished
const upload = require('./Controllers/ImageManagement')
app.use(cookieParser())
app.use(express.static('backend/uploads'))
app.get('/', (req, res) => {
res.send(" CattleTalk Api is active and running...")
})
app.use('/api/ecommerce',Ecommerce);
app.use('/api/admin',AdminRoutes);
app.use('/api/farmers',FarmerRoutes);
app.listen(5002,async()=>{
console.log("The app listening at 5002");
try {
await sequelize.authenticate();
console.log('Database Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
});
我的 package.json 在前端看起来像这样
{
"name": "frontend",
"secure":false,
"version": "0.1.0",
"private": true,
"dependencies": {
"@popperjs/core": "^2.9.1",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"bootstrap": "^5.0.2",
"formik": "^2.2.9",
"js-cookie": "^3.0.1",
"react": "^17.0.1",
"react-bootstrap": "^2.0.0-alpha.2",
"react-bootstrap-sidebar": "^0.0.1",
"react-dom": "^17.0.1",
"react-icons": "^4.2.0",
"react-router": "^5.2.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.1",
"yup": "^0.32.11"
},
"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:5002/"
}
在互联网上探索了几个小时后,我无法得到问题的答案,但调试代码后我发现了问题。实际上,这是 URL 中的一个额外的尾部斜线导致了它的错误行为。 我以前也是这样。
正确的版本是。
额外说明: 所有未从 here should move forward to this link 中找到解决方案并在当前 URL 中找到解决方案的人都在寻找尾部斜线,为了成功代理,您需要消除 URL 末尾的尾部斜线.