问题描述
我在server.js上
const PORT = process.env.PORT || 5000;
我认为这是请求进入端口5000的原因,但是我该如何解决?
我有一个Express应用程序,该应用程序提供响应前端,并向具有相同域名的端点发出请求。
它正在向我想要的some_url / auth发送一个请求,但也向localhost:5000 / auth发送了一个请求,这导致我的应用程序无法正常工作。
即使package.json没有将代理设置为本地主机端口,所以我也无法弄清楚为什么GET和POST请求既要传递给localhost / auth又要传递给some_url / auth,而不仅传递给some_url / auth。
(客户端)app.js:
constructor(props){
super(props);
this.URL = 'https://reactnote-app.herokuapp.com';
this.state = {//some stuff}
}
loginRequest = async () => {
if (!this.state.signup) {
console.log('login request');
const endpoint = this.URL +'/auth'
const res = await
fetch(endpoint,{
method: 'POST',headers : {
'Content-type' : 'application/json'
},body : JSON.stringify({
username : document.getElementsByName('username')[0].value,password : document.getElementsByName('password')[0].value
}
)
});
let body = await res.text();
console.log(body);
this.getUsername();
} else {
this.createuserRequest();
}
}
package.json:
{
"name": "react-notes-app","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","cors": "^2.8.5","express-session": "^1.17.1","md5": "^2.3.0","MysqL": "^2.18.1","node-fetch": "^2.6.0","react": "^16.13.1","react-dom": "^16.13.1","react-scripts": "3.4.3"
},"scripts": {
"start": "node api/server.js"
},"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"
]
},"engines":{
"node":"v12.18.3","npm":"6.14.6"
}
}
解决方法
我没有运行'npm run build',所以它使用的是以前的URL为本地主机的版本。 现在可以工作。