创建react应用程序

by Abhishek Jakhar

通过阿比舍克·贾卡(Abhishek Jakhar)

如何将React应用程序部署到Netlify (How to deploy a React application to Netlify)

I’m going to teach you how to deploy and host your React app with Netlify.Netlify is a service that automates builds, deployments and manages your websites. It’s one of the fastest and easiest deployment solutions these days.

我将教您如何使用Netlify部署和托管React应用。 Netlify是一项服务,可自动构建,部署和管理您的网站。 这是当今最快,最简单的部署解决方案之一。

Netlify offers a free plan. So first, we will log in to Netlify using any one of the options(Github, Gitlab, Bitbucket, Email) given on the login page.

Netlify提供免费计划。 因此,首先,我们将登录Netlify使用的选项登录页面上给出的任何一个(Github上,Gitlab,到位桶,电子邮件)。

We will start by creating a build of our application by running this command:

我们将通过运行以下命令开始创建应用程序的构建:

npm run build

So, now our build folder will get generated which will contain all the production-ready files.

因此,现在将生成我们的构建文件夹,其中将包含所有可用于生产的文件。

Now, there are two ways to deploy our application to Netlify.

现在,有两种方法可以将我们的应用程序部署到Netlify。

拖放 (Drag & Drop)

Netifly has made it so easy that we have to just drag and drop our build folder into their online app (Rightmost image above), and our application will get deployed to a live URL.

Netifly变得如此简单,我们只需要将构建文件夹 拖放到其在线应用程序中 (上面最右边的图像),我们的应用程序就会被部署到实时URL中。

Note: Netlify online app is the screen which appears after you have logged into your netlify account.

注意: Netlify在线应用程序是登录到Netlify帐户后出现的屏幕。

Netlify CLI (Netlify CLI)

Netifly also provides a command line interface that lets you deploy your app straight from the command line. That’s what we will do now.

Netifly还提供了一个命令行界面,使您可以直接从命令行部署应用程序。 那就是我们现在要做的。

So first, we’ll install the CLI using the following command:

因此,首先,我们将使用以下命令安装CLI:

npm install netlify-cli -g

Now, we’re ready to deploy it. To deploy the application we have to make sure that we’re in the project folder and then we will run this command:

现在,我们准备部署它。 要部署应用程序,我们必须确保我们位于项目文件夹中,然后我们将运行以下命令:

netlify deploy

We might get a pop-up window which will ask us to log in with Netlify and grant access to the Netlify CLI.

我们可能会弹出一个窗口,要求我们使用Netlify登录并授予对Netlify CLI的访问权限。

Now, we’ll click Authorize. Now that we’re authorized, we can follow the command line prompts to deploy the app.

现在,我们将单击“授权”。 现在我们已经获得授权,我们可以按照命令行提示来部署应用程序。

命令行提示 (Command Line Prompts)

  1. In the console, it says that “This folder isn’t linked to a site yet. What would you like to do?” It wants to know if we want to link this directory to an existing site or create and configure a new site. Since this is a new site, we’ll select Create & configure a new site.

    在控制台中,它说“ 该文件夹尚未链接到站点。 你想干什么?” 它想知道我们是否要将此目录链接到现有站点或创建并配置新站点。 由于这是一个新站点,因此我们将选择“ 创建并配置一个新站点”。

2. It gives us the option to give our site a name. I’ll type portfolio on netlify (You can type any available name which you like).

2.它使我们可以选择为站点命名。 我将在netlify上键入Portfolio (您可以键入任何可用的名称)。

3. Now it will ask for the Netlify account which you want to use, so I will select my account (Abhishek Jakhar), you can select yours.

3.现在,它将询问您要使用的Netlify帐户,因此我将选择我的帐户(Abhishek Jakhar) ,您可以选择您的帐户

4. Now, as deploy path, we need to specify our project's build directory which contains the assets for deployment. So, we will type build there and press enter.

4.现在,作为部署路径,我们需要指定项目的构建目录,其中包含要部署的资产。 因此,我们将在此处键入build并按Enter。

5. Now, our site will get created and will be deployed to a draft URL first, which we can view by copying and pasting the URL in the browser.

5.现在,将创建我们的网站,并将其首先部署到URL草案中,我们可以通过在浏览器中复制和粘贴URL进行查看。

Now, back in the console, it says “If everything looks good on your draft URL, take it to live with the --prod flag”.

现在,回到控制台,它显示“如果草稿URL上的所有内容看起来都不错,请将其与--prod标志一起使用”

So to make our app live, we’ll run the command shown on the command line

因此,要使我们的应用程序正常运行,我们将运行命令行上显示的命令

netlify deploy --prod

It will ask us one more time to specify the deploy path for the live build which again is our build folder.

它将要求我们再一次指定实时构建的部署路径,该路径也是我们的构建文件夹。

Now, in the console output, we get two URLs. A Unique Deploy URL, which represents the unique URL for each individual deployment, and a Live URL which always displays your latest deployment.

现在,在控制台输出中,我们得到两个URL。 唯一部署URL(代表每个单独部署的唯一URL)和实时URL (始终显示最新部署)。

So each time you update your website and deploy it, you’re going to get a unique URL for that deployment. Basically, if we deploy multiple times we will be having multiple unique URLs so that you can point users to a specific version of your application. But the live URL always displays your latest changes at the same URL.

因此,每次更新并部署网站时,您都将获得该部署的唯一URL。 基本上,如果我们多次部署,我们将拥有多个唯一的URL,以便您可以将用户指向您的应用程序的特定版本 。 但是实时URL始终在同一URL上显示您的最新更改

Note: Netlify automatically secures your site over HTTPS for free.

注:Netlify自动保护您的网站通过HTTPS免费的

找不到页面错误 (Page Not Found Error)

If you’re publishing an app that uses a router like React Router you’ll need to configure redirects and rewrite rules for your URLs. Because when we click on any navigation item to change the page (route) and refresh the browser, we get a 404 error page.

如果您要发布使用诸如React Router之类的路由器的应用,则需要为URL配置重定向和重写规则。 因为当我们单击任何导航项来更改页面(路线)并刷新浏览器时,我们都会看到404错误页面。

So Netlify makes configuring redirects and rewrite rules for your URLs really easy. We’ll need to add a file inside the build folder of our app named _redirects. Inside the file, we need to include the following rewrite rule.

因此,Netlify使配置URL重定向和重写规则变得非常容易。 我们需要在应用程序的build文件夹内添加一个名为_redirects的文件。 在文件内部,我们需要包括以下重写规则。

/*    /index.html  200

This rewrite rule is going to serve index.html file instead of giving a 404, no matter what URL the browser requests.

无论浏览器请求什么URL,此重写规则都将提供index.html文件而不是提供404。

So now, to view the latest changes in the live URL, we need to deploy with netlify deploy. Again, we’ll specify build as the deploy path. Now, when we see the live URL and refresh the app after changing the route we will no longer see the 404 error page.

因此,现在,要查看实时URL中的最新更改,我们需要使用netlify deploy 。 同样,我们将指定build作为部署路径。 现在,当我们看到实时URL并在更改路线后刷新应用程序时,将不再看到404错误页面。

That's all there is to it. On netlify.com you can see your site settings. There you can do stuff like set up a custom domain or connect the site to a GitHub repository.

这里的所有都是它的。 在netlify.com上,您可以看到您的网站设置。 在那里,您可以执行诸如设置自定义域或将站点连接到GitHub存储库之类的工作。

Netlify: All-in-one platform for automating modern web projectsDeploy modern static websites with Netlify. Get CDN, Continuous deployment, 1-click HTTPS, and all the services you…www.netlify.com

Netlify:用于自动化现代Web项目的多合一平台 使用Netlify部署现代静态网站。 获得CDN,持续部署,一键单击HTTPS以及您所需要的所有服务…… www.netlify.com

I hope you’ve found this post informative and helpful. I would love to hear your feedback!

希望您发现这篇文章对您有帮助。 我希望听到您的反馈!

Thank you for reading!

感谢您的阅读!

翻译自: https://www.freecodecamp.org/news/how-to-deploy-a-react-application-to-netlify-363b8a98a985/

创建react应用程序

创建react应用程序_如何将React应用程序部署到Netlify相关推荐

  1. 创建react应用程序_如何使用React创建一个三层应用程序

    创建react应用程序 Discover Functional JavaScript was named one of the best new Functional Programming book ...

  2. react.js做小程序_如何使用React.js构建现代的聊天应用程序

    react.js做小程序 In this tutorial, I will guide you to build your own group chat application using React ...

  3. 如何命令行结束react程序_前端架构React和Vue CTO的选择正确框架的指南

    快速总结:为项目选择正确的javascript框架或库是CTO和项目经理的基本任务.然而,选择的范围很大程度上取决于几个因素,如项目时间.学习曲线.框架性能和团队规模.这篇文章旨在指导他们选择正确的j ...

  4. react 服务器端渲染_服务器端渲染React应用程序的动手指南

    react 服务器端渲染 In the previous article, we described how to make a production build and deploy it to a ...

  5. react前端项目_如何使用React前端设置Ruby on Rails项目

    react前端项目 The author selected the Electronic Frontier Foundation to receive a donation as part of th ...

  6. react前端开发_是的,React正在接管前端开发。 问题是为什么。

    react前端开发 by Samer Buna 通过Samer Buna 是的,React正在接管前端开发. 问题是为什么. (Yes, React is taking over front-end ...

  7. react页面保留_如何在React中保留已登录的用户

    react页面保留 If you run a quick Google search for persisting a logged-in user in React (or keeping a us ...

  8. react hooks使用_何时使用React Suspense和React Hooks

    react hooks使用 React Suspense对Monad就像钩子对应用符号一样 (React Suspense is to a Monad as Hooks are to Applicat ...

  9. react 执行入口_如何使用React执行CRUD操作

    react 执行入口 by Zafar Saleem 通过Zafar Saleem 如何使用React执行CRUD操作 (How to perform CRUD operations using Re ...

最新文章

  1. Android内存溢出分析
  2. 定时任务--mysql数据库备份
  3. Linux 第71天 mariadb backup and recovery
  4. 从 RequireJS 到 SeaJS(2)
  5. SQL Server基本函数
  6. IDEA 公司,又出新神器,一套代码适应多端!
  7. 电脑开机进入setup的四种界面介绍
  8. 做外贸如何免费申请企业邮箱?
  9. 服务链路追踪类型和区别
  10. ExoPlayer在开启循环播放时 统计播放次数和索引
  11. sql三个表关联查询LEFT JOIN
  12. (PDC2008)Anders Hejlsberg: The Future of C#
  13. VR全景制作的行业应用,让生活更加便捷
  14. OpenAI使用Reddit训练聊天机器人
  15. ThinkPad X390拆机
  16. 解决 PageInfo 返回的 total 不正确
  17. cube 设置滴答定时器_CubeMX配置定时器产生指定个数的脉冲
  18. 出纳通如何重新年结?
  19. C#爬取京东手机数据+PowerBI数据可视化展示
  20. <居然讲爬虫>1-爬虫概述

热门文章

  1. 【C++】new A和new A()的区别详解
  2. 事务的隔离级别 mysql
  3. requests模块的操作 0229
  4. Linux完全删除用户
  5. 计算可能的编译结果数
  6. 前端2018年12月9日 前端开发路上的小白心得--励志当个领路人
  7. 代码风格统一: 使用husky, prettier, eslint在代码提交时自动格式化,并检查代码。...
  8. Spring Bean的生命周期(非常详细)
  9. iOS开发——网络使用技术OC篇网络爬虫-使用正则表达式抓取网络数据
  10. 利用RMAN转移裸设备到文件系统