【问题标题】:how to run React + express + node on nginx ec2如何在 nginx ec2 上运行 React + express + node
【发布时间】:2021-06-11 03:12:05
【问题描述】:

在本地机器上一切正常,如果我的服务器设置正确,请告诉我

项目结构

/client/--React frontend app
/client/package.json
/index.js
/package.json -- express server

在我的 /index.js 中

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

const app = express();

app.use(cors());
app.use(express.json());

if (process.env.NODE_ENV === "production") {
    app.use(express.static("client/build"));
    app.get("*", (req, res) => {
        res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
    });
}

app.listen(process.env.B_PORT || 5000, () => {
    console.log(`Express server Up and Running - Port : ${process.env.B_PORT}`);
});

在我的 /client/package.json 中

{
"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
},
    "proxy": "http://localhost:5000",
}

我收到 405 不允许发布请求,但是当我执行 http://myec2ip:5000 时,我可以从 express 获得 json 响应。

我缺少什么配置?

【问题讨论】:

    标签: node.js reactjs express nginx amazon-ec2


    【解决方案1】:

    您需要在 ec2 的安全组设置中打开端口 5000,并允许任何人使用 0.0.0.0 访问它以进行传入连接。

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 2021-04-16
      • 2013-09-20
      • 2021-02-25
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2014-03-30
      相关资源
      最近更新 更多