简体   繁体   English

React 前端连接到我计算机上的开发服务器,而不是 Azure VM 上的节点服务器,它托管在为什么使用 localhost

[英]React front end connects to the development server on my computer instead of the Node server on Azure VM where it is hosted from why using localhost

I have a React front end and Node with Express server.我有一个带有 Express 服务器的 React 前端和 Node。 When I build and run it on my computer everything works perfect.当我在我的计算机上构建并运行它时,一切都很完美。 When I build and run it on my Azure Ubuntu VM the server starts successfully and it hosts the React front end and I can access it no problem.当我在我的 Azure Ubuntu VM 上构建并运行它时,服务器成功启动,它托管了 React 前端,我可以毫无问题地访问它。 But I get a "net::ERR_CONNECTION_REFUSED" in the console when it tries to access the node server.但是当它尝试访问节点服务器时,我在控制台中得到一个“net::ERR_CONNECTION_REFUSED”。 I then noticed that if my server was running on my computer the React app hosted on the Azure VM would hit the server on my local computer and not the one on the Azure VM.然后我注意到,如果我的服务器在我的计算机上运行,则托管在 Azure VM 上的 React 应用程序将访问我本地计算机上的服务器,而不是 Azure VM 上的服务器。

So, how do I get the React app hosted on the VM to properly point to the server/vm it is hosted from?那么,如何让托管在 VM 上的 React 应用程序正确指向托管它的服务器/vm?

The file structure of the app is:该应用程序的文件结构是:

>App Root
  >client
     >build
     >public
     >src
        >components
        >reducer, assets, middleware, services
         App.js
         http-common.js
         index.js
      package.json
  >server
     >config, controllers, models, routes
   package.json
   server.js

server.js服务器.js

const bodyParser = require("body-parser");
const cors = require("cors");
const morgan = require('morgan');
const path = require('path');


const app = express();
app.use(cors());

app.use(bodyParser.json());
app.use(express.static(__dirname + '/client/build'));
app.use(morgan('dev'));

app.use(bodyParser.urlencoded({ extended: true }));

const db = require("./server/models");
db.sequelize.sync();

require("./server/routes/h.routes")(app);
require("./server/routes/p.routes")(app);
require("./server/routes/user.routes")(app);

app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname, '/client/build/index.html'), function(err) {
    console.log('In sendFile of get /*')
    if (err) {
      console.log('error: ', err)
      res.status(500).send(err)
    }
  })
})

const port = 5000;
app.listen(port, () => console.log(`Listening on port ${port}`));

server package.json服务器 package.json

  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "start:dev": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "morgan": "^1.10.0",
    "mysql": "^2.18.1",
    "mysql2": "^2.2.5",
    "path": "^0.12.7",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.3",
    "redux": "^4.0.5",
    "sequelize": "^6.6.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
  "proxy": "http://localhost:5000"
}

http-common.js http-common.js

export default axios.create({
  baseURL: "http://localhost:5000/api",
  headers: {
    "Content-type": "application/json",
  },
});

client package.json客户package.json

  "name": "tissue-screener",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/lab": "^4.0.0-alpha.57",
    "@testing-library/jest-dom": "^5.11.10",
    "@testing-library/react": "^11.2.6",
    "@testing-library/user-event": "^12.8.3",
    "axios": "^0.21.1",
    "bootstrap": "^4.6.0",
    "dotenv": "^8.2.0",
    "msal": "^1.4.10",
    "react": "^17.0.2",
    "react-beautiful-dnd": "^13.1.0",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.3",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.3.0",
    "web-vitals": "^1.1.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"
    ]
  },
  "main": "../server.js",
  "proxy": "http://localhost:5000"
}

I figured out the answer and decided to share it here in case anyone else makes the same dumb mistake I did.我想出了答案,并决定在这里分享,以防其他人犯我犯的同样愚蠢的错误。

http-common.js http-common.js

export default axios.create({
  baseURL: "/api",
  headers: {
    "Content-type": "application/json",
  },
}); 

That's it.而已。 Just had to remove the "http://localhost:5000" from the axios.create and let the proxy setting in the package.json do it's work.只需从 axios.create 中删除“http://localhost:5000”,然后让 package.json 中的代理设置就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将数据从react(前端)传递到节点服务器(后端) - Trying to pass data from react(front end) to node server(backend) CORS 阻止我的节点服务器从反应应用程序本地主机 - CORS blocking my node server from react app localhost 如何使用 Node.js 服务器将我的图像数据从前端保存到 Mongoose 数据库 - How can I save my image data from front end to Mongoose database using Node.js server 如何从节点/ Express服务器提供Vuejs前端文件? - How can I serve my Vuejs front end files from my node/express server? 我的 React 前端无法调用我的 node-express 后端服务器,全栈应用程序部署在 heroku - My React front-end is unable to call my node-express backend server, the fullstack app is deployed in heroku 从前端停止节点服务器中的功能 - Stoping a function in a node server from the front-end 我是否应该在我的前端网站代码(通过 Firebase 托管)中包含我的 NodeJS/Express 服务器代码(托管在 Heroku 上)? - Should I include my NodeJS/Express server code (hosted on Heroku) with my front-end website code (hosted through Firebase)? 为什么我的 JWT expiresIn 值在从服务器发送和在前端接收之间发生变化? - Why does my JWT expiresIn value change between sending it from the server and receiving it on the front-end? 在节点服务器和前端中处理JSON响应 - Processing JSON response in node server and front end 为什么 Express HTTP 服务器从我的本地主机下载 res.end() - Why is Express HTTP server downloading the res.end() from my localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM