如何将ReactJs + Node应用程序部署到远程服务器上? [英] How do I deploy ReactJs + Node app onto remote server?

查看:148
本文介绍了如何将ReactJs + Node应用程序部署到远程服务器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我没有使用任何第三方服务,而只是专用服务器,我该如何做呢?我想以为例。其中有一个构建文件夹。我了解我几乎可以在执行 npm run build public_html 中>,但是存在 / server 文件夹,该文件夹正在使用node并可能与数据库进行交互,无法与其余的build js放在一起。

Assuming I'm not using any third party service, just a dedicated server, how do I go about doing this? I'd like to use this as an example. In it, there's a build folder. I understand I can pretty much just copy paste the contents of this into the public_html of the remote server after doing npm run build but then there's the /server folder which is using node and possibly interacting with the database, can't be put in with the rest of the build js.

我最好的猜测是,我将 npm运行构建并通过FTP将的内容在远程服务器上将文件夹构建到 public_html 文件夹中,然后将服务器文件夹的内容放到另一个子域中,然后SSH到服务器中并执行 npm start 在所述子域中?或者可能将其放在 public_html 的子文件夹中。我来自php背景,曾经使用过js + php,所以任何建议都值得赞赏。

My best guess is that I would npm run build and ftp the contents of the build folder to public_html on the remote server then I would put the contents of the server folder onto another subdomain and then ssh into the server and do npm start in said subdomain? Or possibly have it in a subfolder of the public_html. I'm from a php background and used to vanilla js + php so any advice is much appreciated.

推荐答案

首先创建构建文件夹通过运行

First create build folder in react side by running

npm run build

然后将这些代码行添加到app.js / index.js中的节点侧

then add these lines of code in the node side in app.js/index.js

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

然后将其部署到heroku /远程服务器。

Then deploy it to heroku/remote server.

注意:记得从.gitignore文件中删除/ build行

Note: remember to remove /build line from .gitignore file

这篇关于如何将ReactJs + Node应用程序部署到远程服务器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆