托管在 Express 上的 React 应用程序未通过 Apache ProxyPass 正确路由

时间:2021-06-22 14:49:49

标签: node.js reactjs apache express proxypass

我正在尝试路由流量,以便当您获得 http://example.com/expressApp 时,它会转到我的 Express 应用程序的“/”,该应用程序托管在 AWS 的 8002 端口。然后它会在 /build 中提供静态 React 文件。但是,在加载时,页面是空白的,并且 /static 文件夹中的任何内容都无法识别。我在这里查看了很多类似的答案,但这些解决方案都没有对我有用(或者我可能实施错了?)。

如果我去http://example.com/expressApp/index.html,索引加载正常,但是提交表单时,找不到路由/api/submitForm。

我有以下相关代码:

server.js (express) 中间件:

app.use(bodyParser.json());
app.use('/images', express.static(path.join(__dirname, '/asset_images')));
app.use(express.static('build'));
app.use('/', require('./routes'));

routes.js:

var express = require("express");
var router = express.Router();
const path = require('path');

// Home page

router.get("/", function(req, res) {

  console.log("Reached the webpage!");
  const index = '/build/index.html';
  res.sendFile(__dirname + '/build/index.html', {}, function(err) {
    if (err) {
      console.log("err: ", err);
    }
  });
});


router.post("/api/submitForm", function(req, res) {

    // does some work here


    res.status(200).send(result);

  }).catch((message) => {
    console.log("failure :(");
  });
});

module.exports = router;

Apache ProxyPass 配置:

ProxyPass "/expressApp" "http://example.com:8002"
ProxyPassReverse "/expressApp" "http://example.com:8002"

项目结构:

- express
 -- build
   --- static
   ---- css
   ---- js
  --- index.html
  --- manifest.json
 ...

错误:

Failed to load resource: the server responded with a status of 404 (Not Found)
2.caa79129.chunk.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
2.cf14c61d.chunk.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
main.50aa165f.chunk.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
manifest.json:1 Failed to load resource: the server responded with a status of 404 (Not Found)
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
manifest.json:1 Failed to load resource: the server responded with a status of 404 (Not Found)
2.caa79129.chunk.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
main.49254150.chunk.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)

0 个答案:

没有答案