react部署在node

In this tutorial we will be doing a basic React + Node app deploy to Heroku.

在本教程中,我们将进行基本的React + Node应用程序部署到Heroku。

There are a lot of tutorials that do this only using the command line, so to change things up a bit, I will do it completely without the command line.

有很多教程仅使用命令行来执行此操作,因此,要稍微改变一下内容,我将完全不用命令行来执行此操作。

For things like generating React and Express apps, we have no choice but to use the command line. For everything else we'll use a GUI.

对于生成React和Express应用程序之类的事情,我们别无选择,只能使用命令行。 对于其他所有内容,我们将使用GUI。

I also assume you have a Github and Heroku account. They are both free, so no worries about signing up.

我还假设您有一个Github和Heroku帐户。 它们都是免费的,因此无需担心注册。

sample project:https://github.com/iqbal125/react-express-sample

示例项目: https : //github.com/iqbal125/react-express-示例

React和快速设置 (React and Express Setup)

First, let's start by creating two directories named Server and Client.

首先,让我们开始创建两个名为ServerClient的目录

The Client directory will hold the contents of the create-react-app command, and Server will hold the contents of the express command. This library just creates a simple Express app for us automatically, similar to create-react-app. It needs to be installed globally, which you can do so with the command:

Client目录将保存create-react-app命令的内容,而Server将保存express命令的内容。 这个库只是为我们自动创建一个简单的Express应用,类似于create-react-app 。 它需要全局安装,您可以使用以下命令进行安装:

npm install -g express-generator

npm install -g express-generator

After this, simply run these commands in each of the respective directories to install the starter projects:

之后,只需在每个相应的目录中运行以下命令来安装入门项目:

npx create-react-app app1 in the Client directory

客户端目录中的npx create-react-app app1

express in the Server directory

express服务器目录

Change to the app1 directory generated by create-react-app and run:

转到由create-react-app生成的app1目录,然后运行:

npm run build

npm run build

This will generate a production build version of the project that is optimized for a production deployment, with things like the error handling code and white space removed.

这将生成该项目的生产构建版本,该版本针对生产部署进行了优化,并删除了错误处理代码和空白。

Note: In a development build you would use a proxy to http://localhost:5000 to communicate from your React app to your Express server, but here the React app and the Express server are just one project. The Express server serves the React files.

注意:在开发构建中,您将使用http:// localhost:5000的代理从您的React应用程序与Express服务器进行通信,但是在这里,React应用程序和Express服务器只是一个项目。 Express服务器提供React文件。

Next, cut and paste the entire build directory into the Server directory. Your project structure should look like this:

接下来,将整个构建目录剪切并粘贴到Server目录中。 您的项目结构应如下所示:

We can now add some code to let our Express server know to serve our React project.:

现在,我们可以添加一些代码来让Express服务器知道为我们的React项目提供服务:

....app.use(express.static(path.join(__dirname, 'build')));app.get('/*', (req, res) => {res.sendFile(path.join(__dirname, 'build', 'index.html'));
});....

The first line of code serves all our static files from the build directory.

第一行代码为构建目录中的所有静态文件提供服务。

The second piece of code is to keep our client side routing functional. This code essentially serves the index.html file on any unknown routes. Otherwise we would need to rewrite our entire routing to work with this Express server setup.

第二段代码是保持我们的客户端路由功能正常。 此代码本质上在任何未知路由上提供index.html文件。 否则,我们将需要重写整个路由以与此Express服务器设置一起使用。

To test your app, just run npm start in the Server directory and go to http://localhost 3000 in the browser. Then you should be see your running React app.

要测试您的应用,只需在Server目录中运行npm start并在浏览器中转到http:// localhost 3000 。 然后,您应该会看到正在运行的React应用程序。

Now we are ready to upload this project to GitHub.

现在,我们准备将这个项目上传到GitHub。

的GitHub (GitHub )

Instead of using the command line to upload to GitHub, we will do this with the GUI. First, go to the GitHub homepage and create a new repository. Name it whatever you want, but make sure the Initialize this Repository with a README option checked:

我们将使用GUI来执行此操作,而不是使用命令行将其上传到GitHub。 首先,转到GitHub主页并创建一个新的存储库。 将其命名为任意名称,但请确保选中了使用README初始化此存储库选项:

Next upload all the project files without the node_modules directory.

接下来,上载所有没有node_modules目录的项目文件。

Click commit and we are done. Your uploaded project files will appear on GitHub like so:

单击提交,我们就完成了。 您上传的项目文件将出现在GitHub上,如下所示:

Now we can move on to setting up Heroku.

现在我们可以继续设置Heroku。

Heroku (Heroku)

Go to the Heroku dashboard, create a new app, and name it whatever you like.

转到Heroku仪表板,创建一个新应用,然后根据需要命名。

Next, go to the Deploy tab and select GitHub under Deployment method:

接下来,转到Deploy选项卡,然后在Deployment method下选择GitHub:

If you haven't connected your GitHub account to your Heroku account yet, you will be prompted through the GitHub Auth flow.

如果尚未将GitHub帐户连接到Heroku帐户,则将通过GitHub Auth流程提示您​​。

After this, search for your project on GitHub and connect to it:

之后,在GitHub上搜索您的项目并连接到它:

Finally, we can just deploy our app by clicking the Deploy Branch button:

最后,我们可以通过单击Deploy Branch按钮来部署我们的应用程序:

Heroku will install all the Node modules for you automatically. You can view your project by clicking on the View button.

Heroku将自动为您安装所有Node模块。 您可以通过单击查看按钮来查看您的项目。

And that's it, we're done! Thanks for reading.

就是这样,我们完成了! 谢谢阅读。

Connect with me on Twitter for more updates on future tutorials: https://twitter.com/iqbal125sf

在Twitter上与我联系以获取未来教程的更多更新: https : //twitter.com/iqbal125sf

翻译自: https://www.freecodecamp.org/news/deploy-a-react-node-app-to/

react部署在node

react部署在node_如何在没有命令行的情况下在3分钟内将React + Node应用程序部署到Heroku相关推荐

  1. Kafka教程(一)基础入门:基本概念、安装部署、运维监控、命令行使用

    Kafka教程(一)基础入门 1.基本概念 背景 领英->Apache 分布式.消息发布订阅系统 角色 存储系统 消息系统 流处理平台-Kafka Streaming 特点 高吞吐.低延迟 cg ...

  2. GitHub 开源官方命令行工具登顶 TOP1,5 分钟极速上手!

    官方版的 GitHub CLI 终于问世了,一经开源便火速冲上了 GitHub Trending 榜 TOP1,接下来,就让我们一起来看,如何在短短 5 分钟便可迅速上手玩转 CLI! 作者 | Na ...

  3. c 运行 java linux命令行参数,Linux下用命令行编译运行Java总结

    最近使用腾讯云的Cloud Studio写Java,只能使用命令行进行编译运行,趁此机会,学习一下Linux的一些常用命令.平时windows下IDE用习惯了,现在用命令行进行编译运行,发现其实问题还 ...

  4. linux命令行聊天,Linux 下使用talk 进行聊天

    Linux中talk命令参数程序用于Internet上两个用户之间进行"交谈":通过键盘输入"说话",通过看终端屏幕"聆听".Linux中t ...

  5. centos命令行安装mysql_Centos下安装mysql 总结

    一.MySQL安装 二.MySQL的几个重要目录 MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Li ...

  6. react学习预备知识_在10分钟内学习React基础知识

    react学习预备知识 If you want to learn the basics of React in the time it takes you to drink a cup of coff ...

  7. varnish几个工具命令行工作情况

    varnish通过几个辅助命令行工具观察varnish的工作情况: varnishlog: varnish的日志是写入共享内存的,可以使用varnishlog命令行工具读取 Java代码   [adm ...

  8. linux命令行安装vnc_CentOS下安装VNC并设置远程服务

    首先需要检查一下服务器是否已经安装了VNC服务,检查服务器的是否安装VNC的命令如下: Linux代码 rpm -qa | grep vnc 使用了上面的命令,返回的信息如下所示. 如果没有任何显示, ...

  9. linux中shell命令行缩进,Linux下几个实用的bash命令 | 旺旺知识库

    一.扩展字段匹配 首先我们来看一个叫做扩展字段匹配的功能.这个选项允许你执行比标准Bash所提供的更复杂的字段匹配.例如,你可以定位除了后缀为.tmp之外的所有文件.扩展字段匹配功能可以通过shopt ...

最新文章

  1. 阿里、拼多多P8面试分享!
  2. go mysql socket_Go语言实现socket实例
  3. 在JSP页面中显示List集合·
  4. mysql语句导出数据库文件_通过Mysql命令行语句来导入、导出数据库文件
  5. 分布式搜索elasticsearch集群监控工具bigdesk
  6. mjorm java_MongoDB 的 ORM框架 MJORM
  7. 网页html无图片代码显示图片,BASE64编码方式(Date Url)
  8. 最新创意购物促销海报设计,广告人必看!
  9. win7开放80端口
  10. CocoaPods管理iOS项目 2018年11月06日
  11. 【Kafka】测试Kafka整合Flume
  12. 解决H5的a标签的download属性下载service上的文件/图片出现跨域问题
  13. Windows7磁盘检查与整理的使用
  14. cenos7部署samba
  15. Linux shell 根据时间批量删除指定文件夹下的文件
  16. 适合计算机课堂玩的游戏,几个课堂小游戏(能活跃课堂气氛)
  17. 计算机联锁静态数据表,计算机联锁功能.doc
  18. 发现同义词 python_同义词查找算法
  19. Elastic Stack 开源的大数据解决方案
  20. Vim/Neovim ALE system verilog使用xvlog时出现“ ‘logic‘ is uan unknown type “ 等

热门文章

  1. 【金三银四】启动mysql服务器
  2. HTML如何添加锚点,论程序员成长的正确姿势
  3. BZOJ1014: [JSOI2008]火星人prefix
  4. 《量化投资:以MATLAB为工具》连载(1)基础篇-N分钟学会MATLAB(上)
  5. ExtJS Grid Column Number
  6. 游戏组件——挑战:创建NextBlock游戏组件
  7. ANTLR和StringTemplate
  8. Java多线程 ——线程基础和锁锁锁
  9. 如何一键部署项目、代码自动更新
  10. LAMP介绍-MySQL安装