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

创建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应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要将React应用部署Gitee Pages,可以按照以下步骤操作: 1. 将React应用打包为静态文件。可以使用以下命令在应用的根目录下运行: ``` npm run build ``` 这将在`build`目录中生成应用的静态文件。 2. 创建一个名为`gh-pages`的分支。可以使用以下命令在本地创建并切换到`gh-pages`分支: ``` git checkout -b gh-pages ``` 3. 将`build`目录下的所有文件添加到`gh-pages`分支。可以使用以下命令: ``` git add build/* ``` 4. 提交更改并推送到Gitee仓库: ``` git commit -m "Deploy to Gitee Pages" git push origin gh-pages ``` 5. 在Gitee仓库中打开“设置”选项卡,向下滚动到“Gitee Pages”部分。 6. 在“源”下拉菜单中选择`gh-pages`分支,并选择`/`作为部署路径。 7. 单击“保存”按钮,Gitee Pages将开始构建并部署您的React应用。部署完成后,您可以访问`https://<username>.gitee.io/<repository-name>/`来查看应用。 请注意,构建和部署可能需要几分钟时间。如果您遇到任何错误或问题,请确保已正确配置您的Gitee仓库,并检查Gitee Pages的构建日志以获取更多详细信息。 ### 回答2: 将 React 应用部署Gitee 平台需要以下步骤: 1. 在 Gitee 上创建一个新的项目仓库,选择"空仓库"的选项。 2. 将你的 React 应用的代码上传到该仓库。你可以使用 Git 命令行或 Gitee 提供的仓库管理工具将代码推送到远程仓库。 3. 在项目根目录下,创建一个名为 `.gitignore` 的文件,将不需要上传到仓库的文件和文件夹添加到该文件中,例如 `node_modules` 文件夹。 4. 使用终端进入项目根目录,运行以下命令安装项目的所有依赖:`npm install` 或 `yarn install`。 5. 在命令行中运行 `npm run build` 或 `yarn build`,以生成部署所需的生产版本的代码。 6. 在生成的 `build` 文件夹中的所有文件是应用的生产版本。将这些文件夹和文件上传到 Gitee 仓库。 7. 在 Gitee 仓库页面中,找到部署设置选项。选择适合你应用类型的部署选项。对于一个 React 应用,可以选择静态页面部署选项。 8. 配置部署选项,指定正确的访问路径和部署的分支。按照Gitee的说明,配置你的部署选择。 9. 完成配置后,点击保存并部署Gitee 将自动完成从项目仓库中拉取代码、安装依赖、构建和部署的过程。 10. 完成部署后,Gitee 将为你的应用生成一个访问链接,你可以通过该链接访问和共享你的 React 应用。 以上是将 React 应用部署Gitee 的大致步骤,根据你的应用需求和 Gitee 平台的具体配置可能会有所差异。建议在部署之前,详细阅读 Gitee 平台相关的文档和指南。 ### 回答3: 将 React 应用部署Gitee 的步骤如下: 1. 首先,在 Gitee 上创建一个新的仓库,用来存放你的 React 应用代码。在 Gitee 的主页上点击"创建仓库"按钮,填写仓库名称和描述,选择仓库可见性,然后点击"创建仓库"按钮完成创建。 2. 在本地计算机上,进入你的 React 应用的代码目录。 3. 初始化 Git 仓库,并将你的应用代码添加到仓库中。在终端中执行以下命令: ```shell git init git add . git commit -m "Initial commit" ``` 4. 在 Gitee 上的仓库页面中,复制仓库的 SSH 或 HTTPS 地址。 5. 在终端中执行以下命令,将本地仓库与 Gitee 远程仓库进行关联: ```shell git remote add origin <Gitee 仓库地址> ``` (Gitee 仓库地址是步骤4中复制的地址) 6. 提交代码到 Gitee 远程仓库: ```shell git push origin master ``` 7. 等待代码推送完成后,刷新 Gitee 仓库页面,即可看到你的 React 应用代码已经成功部署Gitee 上。 请注意,以上步骤假设你的计算机上已经配置了 Git,并且你已经在 Gitee 上创建了账户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值