部署在 Google Cloud Run 上的 Node.js 应用遇到“找不到文件”错误

问题描述 投票:0回答:1

尝试使用 google cloud run 在 Node.js 服务器上部署四个 Web 应用程序时遇到“404 文件未找到”错误。一切都在本地主机中按预期工作。

这是我的节点服务器的脚本:

const express = require('express');
const path = require('path');
const app = express();
const PORT = 9000;

// Middleware to parse JSON requests
app.use(express.json());

const superadminBuildPath = path.join(__dirname, './superadmin/build');
const clubadminBuildPath = path.join(__dirname, './clubadmin/build');
const clubcastLPBuildPath = path.join(__dirname, './static-lp');
const clubsLPBuildPath = path.join(__dirname, './clubs-lp/dist');

// Serve static content of superadmin
app.use('/superadmin', express.static(superadminBuildPath));

// Serve static content of clubadmin
app.use('/clubadmin', express.static(clubadminBuildPath));

// Serve static content of static LP
app.use(express.static(clubcastLPBuildPath));

// Serve static content of clubs LP
app.use('/clubs', express.static(clubsLPBuildPath));


app.get('/clubadmin/*', function (req, res) {
    res.sendFile(path.join(clubadminBuildPath, 'index.html'));
});

app.get('/superadmin/*', function (req, res) {
    res.sendFile(path.join(superadminBuildPath, 'index.html'));
});

app.get('/clubs/*', function (req, res) {
    res.sendFile(path.join(clubsLPBuildPath, 'index.html'));
});

app.get('/*', function (req, res) {
    res.sendFile(path.join(clubcastLPBuildPath, 'index.html'));
});


// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

注意:clubadmin、supadmin 和clubs lp 是React 应用程序,而static lp 只是使用HTML、CSS 和JS 构建的静态站点。

如果您能提供任何帮助,我们将不胜感激。预先感谢您!

reactjs node.js google-cloud-platform gcloud google-cloud-run
1个回答
0
投票

既然您寻求任何帮助,请参阅我的以下建议:

  • 如果您使用谷歌云从源代码运行部署检查这个
  • 如果您使用 Dockerfile 进行部署,请确保在 Dockerfile 的构建步骤中添加用于复制这些文件夹的命令。

您还可以使用带有云运行的卷来提供静态内容。请参阅这个 stackoverflow 答案

© www.soinside.com 2019 - 2024. All rights reserved.