heroku_如何将节点应用程序和数据库部署到Heroku

heroku

Heroku is a cloud-based, fully-managed platform as a service (PaaS) for building, running, and managing apps. The platform is flexible and designed with DX support for you and your team’s preferred development style and to help you stay focused and productive.

Heroku是基于云的,完全托管的平台即服务(PaaS),用于构建,运行和管理应用程序。 该平台是灵活的,具有DX支持,可为您和您的团队的首选开发风格设计,并帮助您保持专注和高效。

Developers, teams, and businesses of all sizes use Heroku to deploy, manage, and scale apps. Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly.

各种规模的开发人员,团队和企业都使用Heroku来部署,管理和扩展应用程序。 无论您是构建简单的原型还是关键业务产品,Heroku的完全托管平台都为您提供了最简单的途径来快速交付应用程序。

With features like Heroku Runtime, Heroku Postgres (SQL), Heroku Redis, Add-ons, Data Clips, App metrics, Smart containers, Enterprise-grade support, GitHub Integration and lots more, Heroku gives developers the freedom to focus on their core product without the distraction of maintaining servers, hardware, or infrastructure.

借助Heroku Runtime,Heroku Postgres(SQL),Heroku Redis,附加组件,数据剪辑,App指标,智能容器,企业级支持,GitHub集成等功能,Heroku使开发人员可以自由地专注于其核心产品无需分心维护服务器,硬件或基础架构。



One of Heroku's core feature is deploying, managing, and scaling apps with your favorite languages [Node, Ruby, Python, Java, PHP, Go, and more].In this article, I'll show you how to take an existing Node.js app and deploy it to Heroku – everything from creating your Heroku account to adding a database to your deployed application.

Heroku的核心功能之一就是使用您喜欢的语言(Node,Ruby,Python,Java,PHP,Go等)部署,管理和扩展应用程序。在本文中,我将向您展示如何使用现有Node。 js应用程序并将其部署到Heroku –从创建您的Heroku帐户到将数据库添加到已部署的应用程序的所有过程。

先决条件 (Prerequisites)

In my previous article, I wrote about "Building a SlackBot with Node.js and SlackBots.js" and I promised to write a follow-up article to show how to host the SlackBot on either Heroku, Zeit or Netlify and publish it to the Slack Apps store. Well, this is the follow-up article but without the "Publishing to Slack Apps" part. We'll cover that in another article.

在我以前的文章中,我写过“ 使用Node.js和SlackBots.js构建SlackBot ”,并答应写一篇后续文章,展示如何在Heroku,Zeit或Netlify上托管SlackBot并将其发布到Slack Apps商店。 好吧,这是后续文章,但没有“发布到Slack应用程序”部分。 我们将在另一篇文章中介绍。

I assume you have/ know the following already:

我假设您已经/知道以下信息:

奖金 (Bonus)

If you don't have npm, Node, and Heroku CLI installed or a Heroku account already, here's a quick bonus [ Yes, you're welcome :) ].

如果您尚未安装npm,Node和Heroku CLI,或者尚未安装Heroku帐户,则可以享受快速赠金[是的,欢迎您:)]。

安装npm和Node (Installing npm and Node)

  • Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.

    Node.js是基于Chrome的V8 JavaScript引擎构建JavaScript运行时。

  • npm is the package manager for Node.js. An open-source project created to help JavaScript developers easily share packaged modules of code.

    npm是Node.js的软件包管理器。 创建了一个开源项目来帮助JavaScript开发人员轻松共享打包的代码模块。

You can simply download Node.js here. Don't worry, npm comes with Node.js, so doing this installs both ✨

您可以在这里简单地下载Node.js。 不用担心,npm随Node.js一起提供,因此这样做会同时安装✨

创建一个免费的Heroku帐户 (Creating a free Heroku account)

Kindly head here and fill the Signup form. It's pretty simple.

请前往此处并填写注册表单。 很简单

安装Heroku CLI (Installing Heroku CLI)

The Heroku Command Line Interface (CLI) makes it easy to create and manage your Heroku apps directly from the terminal. It’s an essential part of using Heroku. [ Well, you can decide to use the GitHub integration feature and Heroku Dashboard but yes you should learn how to use the CLI ]Heroku CLI requires Git, the popular version control system. If you don’t already have Git installed, I wrote this article to help you.

Heroku命令行界面(CLI)使得直接从终端轻松创建和管理Heroku应用程序变得容易。 这是使用Heroku的必要部分。 [嗯,您可以决定使用GitHub集成功能和Heroku仪表板,但是的,您应该学习如何使用CLI。] Heroku CLI需要流行的版本控制系统Git。 如果您尚未安装Git,我将为您写这篇文章

适用于Mac OS的Heroku CLI (Heroku CLI for Mac OS)
brew tap heroku/brew && brew install heroku

or download the installer.

下载安装程序

适用于Ubuntu的Heroku CLI (Heroku CLI for Ubuntu)
sudo snap install --classic heroku
Windows版Heroku CLI (Heroku CLI for Windows)

Download the installer for 64-Bit or 32-Bit.

下载64位32位安装程序。

其他安装方式 (Other installation methods)

Please read this.

请阅读

Heroku CLI入门 (Getting started with Heroku CLI)
  • Verify your installation

    验证您的安装
heroku --version

heroku/7.30.1 linux-x64 node-v11.14.0

heroku / 7.30.1 linux-x64节点-v11.14.0

  • Login to your Heroku account

    登录到您的Heroku帐户

There are two ways to do this:

有两种方法可以做到这一点:

  • Web based auth

    基于Web的身份验证

heroku login

Follow the instructions and login via your web browser then return to your terminal.

按照说明进行操作,并通过Web浏览器登录,然后返回到终端。

  • CLI auth

    CLI验证

This is a safer option as it saves your email address and an API token to ~/.netrc for future use.

这是一个更安全的选择,因为它将您的电子邮件地址和API令牌保存到~/.netrc以备将来使用。

heroku login -i


部署Node.js应用 (Deploying your Node.js App)

I presume you've built the SlackBot already. If you haven't, please clone the finished project.

我想您已经构建了SlackBot。 如果还没有,请克隆完成的项目

The project is a simple Slackbot that displays random inspiring techie quotes and jokes for developers/designers.

该项目是一个简单的Slackbot,它为开发人员/设计人员显示随机启发的技术报价和笑话。

git clone https://github.com/BolajiAyodeji/inspireNuggetsSlackBot.git && cd inspireNuggetsSlackBot

Now let's deploy our app to Heroku 🎉🎉. I'll show you two ways to do this:

现在,将我们的应用程序部署到Heroku🎉🎉。 我将向您展示两种方法:

通过Heroku Git进行部署 (Deploy via the Heroku Git)

This is done via the Heroku CLI.

这是通过Heroku CLI完成的。

☑️清单 (☑️ Checklist)
  • Specify the version of Node.js that will be used to run your application on Heroku in your package.json file.

    package.json文件中指定将用于在Heroku上运行应用程序的Node.js版本。

"engines": {
    "node": "10.16.0"
  },
  • Specify your start script.

    指定您的启动脚本。

    Simply create a

    只需创建一个

    Procfile (without any file extension) and add

    Procfile (不带任何文件扩展名)并添加

web: node index.js

Heroku first looks for this Procfile. If none is found, Heroku will attempt to start a default web process via the start script in your package.json.

Heroku首先寻找该Procfile。 如果未找到任何内容,Heroku将尝试通过package.json的启动脚本启动默认的Web进程。

  • Start your app locally using the heroku local command to be sure everything works fine

    使用heroku local命令在本地启动您的应用,以确保一切正常
heroku local web

Your app should now be running on http://localhost:5000.

您的应用现在应该在http:// localhost:5000上运行

  • Don't forget to .gitignore

    不要忘记.gitignore

/node_modules
.DS_Store
/*.env
🚀部署 (🚀 Let's Deploy)

How this works is, you have the project working on local already and you've pushed to GitHub already.

这是如何工作的,您已经使该项目在本地工作,并且已经推送到GitHub。

  • Run heroku create

    运行heroku create

Basically, this command creates a new Heroku app for you with some randomly generated domain and adds Heroku to your local Git repository.

基本上,此命令会使用随机生成的域为您创建一个新的Heroku应用,并将Heroku添加到您的本地Git存储库中。

  • Now run git push heroku master

    现在运行git push heroku master

This is the magic command, it pushes your app to Heroku, installs it there, and launches it on your allocated domain.

这是魔术命令,它将您的应用程序推送到Heroku,在此处安装它,然后在您分配的域上启动它。

In the example above, it's https://lit-cove-58897.herokuapp.com/

在上面的示例中,它是https://lit-cove-58897.herokuapp.com/

You can always make changes to your app settings and domains in your Heroku Dashboard

您始终可以在Heroku信息中心中更改应用程序设置和域

  • Now visit your app in your browser

    现在在浏览器中访问您的应用
heroku open
  • You can also view information about your running app using one of the logging commands. This is very useful in debugging errors.

    您还可以使用其中一种日志记录命令查看有关正在运行的应用程序的信息。 这对于调试错误非常有用。
heroku logs --tail
通过GitHub集成进行部署 (Deploy via GitHub integration)

You can configure GitHub integration in the Deploy tab of apps in the Heroku Dashboard.

您可以在Heroku仪表板的应用程序的Deploy选项卡中配置GitHub集成。

☑️清单 (☑️ Checklist)
  • All previous checklists apply here – ensure you have the app deployed to GitHub already

    先前所有清单均适用于此–确保已将应用程序部署到GitHub
🚀部署 (🚀 Let's Deploy)

How this method works is that you push your entire project to GitHub and integrate it to Heroku. Every time you push, it deploys from GitHub to Heroku. Pretty cool right?

该方法的工作原理是将整个项目推送到GitHub并将其集成到Heroku。 每次您推送时,它都会从GitHub部署到Heroku。 很酷吧?

  • Login to your Heroku Dashboard and create a new app

    登录到您的Heroku仪表板并创建一个新应用
  • Select your app name and region

    选择您的应用名称和地区

Now your app has successfully been created

现在,您的应用已成功创建

  • Click the deploy tab and scroll to the Deployment method section

    单击部署选项卡,然后滚动到“ 部署方法”部分

  • Click the Connect to GitHub button

    单击连接到GitHub按钮

  • Now you have the Connect to GitHub section, search for the repository and deploy.

    现在,您具有“ 连接到GitHub”部分 ,搜索存储库并进行部署。

  • Now your app was deployed successfully

    现在您的应用已成功部署
自动部署 (Automatic deploys)

Now your app is deployed but you'll have to keep deploying manually. You need to enable automatic deploys for a GitHub branch, so Heroku builds and deploys all pushes to that branch.

现在,您的应用已部署完毕,但您必须继续手动进行部署。 您需要为GitHub分支启用自动部署,以便Heroku构建并将所有推送部署到该分支。

  • Scroll to the Automatic Deploys section

    滚动到“ 自动部署”部分

Select the branch you want to deploy. Ideally, this should be the master branch but change this according to your preference.

选择要部署的分支。 理想情况下,这应该是master分支,但可以根据自己的喜好进行更改。

Now every push to master (or the branch you chose) will deploy a new version of this app.

现在,每次推送至master (或您选择的分支)将部署此应用程序的新版本。

Node.js构建包 (Node.js Buildpack)

In Heroku, Buildpacks are scripts that are run when your app is deployed. They are used to install dependencies for your app and configure your environment.

在Heroku中,Buildpacks是在部署应用程序时运行的脚本。 它们用于为您的应用安装依赖项并配置您的环境。

After deploying your app, ensure you add a Node.js buildpack to your project.

部署应用程序后,请确保将Node.js buildpack添加到项目中。

  • Go to Settings and scroll to the Buildpack section

    转到设置并滚动到Buildpack部分

  • Click the Add Buildpack button and select Node.js in the Popup modal.

    单击添加构建包按钮,然后在弹出模式中选择Node.js。

  • Now the new buildpack configuration will be used when this app is next deployed. Make some changes to your app and push to GitHub – it will automatically deploy.

    现在,下次部署此应用程序时将使用新的buildpack配置。 对您的应用程序进行一些更改,然后推送到GitHub-它会自动部署。

将数据库添加到已部署的应用程序中 (Adding a Database to your deployed App')

The Heroku add-on marketplace has a large number of data stores, from Redis and MongoDB providers, to Postgres and MySQL.

Heroku附加市场上有大量数据存储,从Redis和MongoDB提供程序到Postgres和MySQL。

Heroku provides three managed data services to all customers in the form of Add-ons:

Heroku以附件的形式向所有客户提供三种托管数据服务:

Writing about this three will make this article too long. It's pretty simple and I'll add some links to the Heroku Docs.

写这三篇文章会使本文太长。 这非常简单,我将向Heroku Docs添加一些链接。



结论 (Conclusion)

Every Heroku account is allocated a pool of free dyno hours. Heroku (free) dynos are great for hosting apps and personal projects. The downside, however, is that your app will fall asleep if it doesn't receive any web traffic within 30-minutes :(.

每个Heroku帐户都分配有免费的动态小时池。 Heroku(免费)测功机非常适合托管应用程序和个人项目。 但是,不利的一面是,如果您的应用在30分钟内没有收到任何网络流量,就会使其进入睡眠状态:(。

You can use external tools to ping your server periodically so it never falls asleep.

您可以使用外部工具定期对服务器执行ping操作,以使其永远不会进入睡眠状态。

Here are some to consider:

这里有一些要考虑的问题:



Heroku is meticulously designed to help developers be as productive as possible. The platform removes frustrating obstacles and mundane tasks, so you can stay free of distraction in your development flow. Wherever you are on the learning path, Heroku helps you love app development even more. - Heroku
Heroku经过精心设计,旨在帮助开发人员提高生产力。 该平台消除了令人沮丧的障碍和平凡的任务,因此您可以在开发流程中不受干扰。 无论您在学习之路上的何处,Heroku都能帮助您更加热爱应用程序开发。 -Heroku

The Heroku experience provides services, tools, workflows, and polyglot support—all designed to enhance developer productivity. There is more to using Heroku and I hope you explore more and build amazing stuff with Heroku.

Heroku的经验提供了服务,工具,工作流和多语言支持-旨在提高开发人员的生产力。 使用Heroku还有更多功能,希望您能探索更多内容并使用Heroku构建令人惊奇的东西。

If you're a student, Kindly register for the GitHub Student Developer Pack to get One free Hobby Dyno for up to two years.

如果您是学生,请注册GitHub Student Developer Pack,以获取一个免费的Hobby Dyno ,最多两年。

The pack give students free access to the best developer tools in one place so you can learn by doing.

该工具包使学生可以在一处自由访问最好的开发人员工具,因此您可以边做边学。

翻译自: https://www.freecodecamp.org/news/how-to-deploy-a-node-application-and-database-to-heroku/

heroku

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值