如何使用 Node js 后端托管应用程序 React js。在互联网上找不到任何东西。我需要运行
build
后台命令?请帮助我。
提前致谢。
我建议您使用Heroku,您可以直接从 GitHub 存储库免费托管全栈应用程序,每当您在存储库上推送某些内容时,它都会自动重新部署。
唯一的轻微缺点是,如果您的应用程序有一段时间没有被访问并变得空闲(如果您使用免费选项),则必须等待大约 5 秒服务器才能启动。
有很多关于如何操作的教程。
至于在生产中为您的应用程序提供静态版本 - 这可能有用:
服务器.js
/* If in production mode - serve compressed/static react content to server. i.e. what would be otherwise localhost:5000 would display frontend content.
/!\ Do not forget to generate Procfile and script for Heroku to insure proper generation of "build" directory /!\ */
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "../frontend/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "../frontend", "build", "index.html"));
});
}
如果你告诉你的服务器使用上面的代码提供静态版本,Heroku 会自动处理它。
还有 Glitch.com
我的做法: 我构建了我的 React 项目并将其托管在服务器上。 至于 Node js,我自己运行它,并使用 Pm2 在服务器上运行它(https://pm2.keymetrics.io/docs/usage/quick-start/),但是还有很多其他方法谷歌上就能找到。
希望我回答了你的问题