尝试使用 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 构建的静态站点。
如果您能提供任何帮助,我们将不胜感激。预先感谢您!
既然您寻求任何帮助,请参阅我的以下建议:
您还可以使用带有云运行的卷来提供静态内容。请参阅这个 stackoverflow 答案。