简体   繁体   English

在同一台机器上设置 react 和 nodejs 服务器

[英]Setting up react and nodejs servers on same machine

I am setting up a reactjs application on port 3000 as well as a nodejs API server on port 3500 on the same box on the internet.我正在端口 3000 上设置 reactjs 应用程序,以及在 Internet 上同一个盒子的端口 3500 上设置 nodejs API 服务器。 Assume the box has a domain name example.com, and I am using nginx in reverse proxy to receive the end users over https, and internally direct it to the port 3000 of the reactjs server. Assume the box has a domain name example.com, and I am using nginx in reverse proxy to receive the end users over https, and internally direct it to the port 3000 of the reactjs server. On the react code, while calling axios API for a get command, which of the following should I be using:在反应代码上,在调用 axios API 获取命令时,我应该使用以下哪个:

Few tests I did:我做了几个测试:

  • Access from my browser the reactjs application successfully as example.com (nginx does the mapping to port 3000)从我的浏览器访问 reactjs 应用程序成功作为 example.com (nginx 映射到端口 3000)
  • Using https://reqbin.com/ I was able to access the nodejs server API and get the correct result using: http://example.com:3500/users Using https://reqbin.com/ I was able to access the nodejs server API and get the correct result using: http://example.com:3500/users
  • Using https instead of http causes an error: SSL connection error Error code: 10035使用 https 而不是 http 会导致错误:SSL 连接错误 错误代码:10035

If end user is supposed to connect over https to the react server, then the react server as well as the nodejs server should be running in https mode, or the browser will block the request and it will never reach the server.如果最终用户应该通过 https 连接到 react 服务器,那么 react 服务器和 nodejs 服务器应该在 https 模式下运行,否则浏览器将阻止请求并且它永远不会到达服务器。 Here is how to:以下是如何:

  • Run the react server in https mode:在 https 模式下运行反应服务器:
  1. Change nginx reverse proxy configuration to be:将 nginx 反向代理配置更改为:

         proxy_pass https://localhost:3000;

  1. Changed the URL for the nodejs server that axios is calling from http://localhost:3500 to https://example.com:3500 Changed the URL for the nodejs server that axios is calling from http://localhost:3500 to https://example.com:3500

  2. After npm run build, and upload the build directory to the server, run the following commands: npm运行build后,将build目录上传到服务器,运行如下命令:

su
serve -s build --listen 3000 --ssl-cert "/etc/letsencrypt/live/example.com/fullchain.pem" --ssl-key "/etc/letsencrypt/live/example.com/privkey.pem"
  • Run the nodejs server in https mode:在 https 模式下运行 nodejs 服务器:
  1. Change the code of server.js with the following:使用以下代码更改 server.js 的代码:
    const https = require('https');
    const fs = require('fs');
    
    const options = {
      key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
      cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
    };
    
    https.createServer(options, app).listen(PORT, ()=>{
       console.log(`Server running https on port ${PORT}`)
      });

  1. Run the following commands:运行以下命令:

    su
    node server

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM