heroku链接node_在Heroku上将MongoDB与Node.js应用程序一起使用

heroku链接node

On this blog post I’ll use a JS MVC ToDo List to show you step by step how to deploy an application using Node.js with Express and Mongoose on Heroku. There are many ToDo app examples over there. All of them are basically client side source code, which means you have to create a server file in order to deploy it to Heroku. A good news is this file is already created, and the another good new is that server file can be used in almost all ToDo apps listed there. This file gives you the flexiblity to use any JavaScript MVC from your choice. I’m gonna use the one built by AngularJS framework.

在这篇博客文章中,我将使用JS MVC任务列表向您逐步展示如何在Heroku上使用带有Express和Mongoose的Node.js部署应用程序。 有很多待办事项应用的例子在那里 。 它们基本上都是客户端源代码 ,这意味着您必须创建服务器文件才能将其部署到Heroku。 好消息是该文件已经创建,另一个好消息是该服务器文件几乎可以在此处列出的所有ToDo应用中使用。 该文件使您可以灵活选择使用任何JavaScript MVC。 我将使用AngularJS框架构建的框架。

Based on this MVC AngularJS ToDo List, I created a new github repository to be easier to deploy it on Heroku. If you’re unfamiliar with those technologies, now would be an excellent time to go through this great start. And for you, developer, who’s familiar with JavaScript, for sure it’ll not be hard. Go check it out!

基于此MVC AngularJS待办事项列表 ,我创建了一个新的github存储库 ,以便更轻松地将其部署在Heroku上。 如果您不熟悉这些技术,那么现在将是度过美好起点的绝佳时机。 对于您来说,熟悉JavaScript的开发人员肯定不会难。 去看看吧!

在Heroku和MongoLab上设置免费帐户 (Setting up a free account on Heroku and MongoLab)

Before I get into the code and walk you through it, I wanted to talk about how to create your free account on Heroku and set it up: (i) first of all sign up, (ii) after that create your first app and (iii) then register and add a public SSH key to your account. You can check this guide out for further details about generating SSH keys.

在开始学习代码并逐步入门之前,我想谈谈如何在Heroku上创建免费帐户并进行设置:(i)首先注册 ,(ii)创建第一个应用程序,然后( iii)然后注册一个公共SSH密钥并将其添加到您的帐户 。 您可以查看本指南以获取有关生成SSH密钥的更多详细信息。

The next step is to have a Mongo database running. It’s NOT required but it’s a necessary step to test it locally before deploying. To simplify let's use a MongoDB as a Service using MongoLab. Feel free to sign up and create your own database.

下一步是运行Mongo数据库。 它不是必需的,但这是在部署之前在本地对其进行测试的必要步骤。 为了简化,让我们使用MongoLab将MongoDB用作服务。 随意注册并创建自己的数据库

在本地运行示例 (Running the example locally)

The source code is available on my GitHub, clone it with:

源代码可在我的GitHub上找到 ,并使用以下命令对其进行克隆:

git clone git@github.com:igorlima/deploy-express-with-mongoose-on-heroku.git

To run it locally, install all dependencies needed with npm install. And set a new environment variable: MONGOLAB_URI. It should contain all the MongoDB connection information needed to connect to your database. You may also specify several more parameters in the uri depending on your environment. In this example, we are gonna use the following standard: mongodb://username:password@host:port/database

要在本地运行,请安装npm install所需的所有依赖项。 并设置一个新的环境变量: MONGOLAB_URI 。 它应包含连接到数据库所需的所有MongoDB连接信息 。 您还可以根据环境在uri中指定其他几个参数 。 在此示例中,我们将使用以下标准: mongodb://username:password@host:port/database

I’m gonna share my credential here. Feel free to use it while you’re learning. After that, create and use your own credential. Thanks.

我要在这里分享我的证书。 在学习时随时使用它。 之后,创建并使用您自己的证书。 谢谢。

For Mac/Linux users, you can simply type:

对于Mac / Linux用户 ,您只需键入:

export MONGOLAB_URI="mongodb://example:example@ds053312.mongolab.com:53312/todolist"

For Windows users, do:

对于Windows用户 ,请执行以下操作:

SET MONGOLAB_URI="mongodb://example:example@ds053312.mongolab.com:53312/todolist"

After setting up this environment variable, run via terminal node server.js. It should be available at the port 5000, then open any browser and reach http://localhost:5000/.

设置此环境变量后,通过终端node server.js运行。 它应该在端口5000上可用,然后打开任何浏览器并访问http://localhost:5000/

在Heroku上部署 (Deploying on Heroku)

You can easily use a git repo to deploy to Heroku, you just need to add a new remote git repository by using the git remote add command on the terminal. To know your app remote URL you have to go to your Dashboard, click on the app you have created in the session above, then click on Settings link. You’ll be able to see the Git URL, copy and use it in the command below:

您可以轻松地使用git repo部署到Heroku,只需在终端上使用git remote add命令添加新的远程git存储库。 要知道您的应用程序远程URL,您必须转到“ 仪表板” ,单击在上面的会话中创建的应用程序,然后单击“ 设置”链接。 您将能够看到Git URL ,然后在以下命令中复制并使用它:

git remote add heroku git@heroku.com:<your-heroku-app-name>.git

Now that we have everything in order, go ahead and push to Heroku!

现在我们一切就绪,继续前进到Heroku!

git push heroku HEAD:master

Notice this example app has a file named Procfile. It’s a text file in the root directory to explicitly declare what command should be executed to start your app. The Procfile in this example app you deployed looks like this:

请注意,此示例应用程序具有一个名为Procfile的文件。 这是根目录中的一个文本文件,用于明确声明应执行什么命令来启动您的应用。 在此示例应用程序中部署的Procfile如下所示:

web: node server.js

I wanna take a moment and mention Heroku tool belt. A command line utility to access Heroku Platform API for creating/renaming apps, running one-off dynos, taking backups, and configuring add-ons. Check out this blog post to know all steps required to deploy to Heroku by using Heroku tool belt.

我想花点时间提及Heroku工具带 。 命令行实用程序,用于访问Heroku Platform API,以创建/重命名应用程序,运行一次性测功,进行备份以及配置加载项。 查看此博客文章,以了解使用Heroku工具带部署到Heroku所需的所有步骤。

添加一个MongoDB插件 (Adding a MongoDB add-on)

Now it’s time to add the MongoLab add-on. To do that you can simply go to your Dashboard, click on the app just created by you, open the Resources tab and then add a new add-on clicking on EDIT or PLUS button under Add-ons session. See images below:

现在是时候添加MongoLab插件了 。 要做到这一点,你可以简单地去您的仪表盘 ,单击刚刚创建的应用程序,打开资源选项卡,然后添加一个新的附加点击下加载项会话中编辑加号按钮。 见下图:

IMG_0890_zpstuzzdx0b

IMG_0891_zpsudvl1cak

There many MongoDB services, I choose MongoLab because one reason: it’s free. To find more add-ons, feel free to see all at this link.

MongoDB服务很多,我选择MongoLab是因为一个原因:它是免费的。 要查找更多插件 ,请随时在此链接中查看全部。

The MongoLab add-on contributes one config variable to your Heroku environment: MONGOLAB_URI. This variable is a URI containing all the MongoDB connection information needed to connect to your database.

MongoLab附加组件为您的Heroku环境提供了一个配置变量: MONGOLAB_URI 。 此变量是一个URI,其中包含连接到数据库所需的所有MongoDB连接信息。

Most client libraries support using the URI directly. However, some will require pulling the components of the URI apart.

大多数客户端库都支持直接使用URI。 但是,有些将需要将URI的各个部分分开。

浏览愉快 (Happy browsing)

We’re finally at the part that we’ve wanted this entire time. Seeing the application in browser! Go ahead and visit that in browser: http://<your-heroku-app-name>.heroku-app.com

我们终于在整个过程中都想要得到这一部分。 在浏览器中看到该应用程序 ! 继续并在浏览器中访问它: http://<your-heroku-app-name>.heroku-app.com

It’s magic! We have our MVC ToDo List app in the browser just like we wanted.

这是魔法! 就像我们想要的那样,我们在浏览器中提供了MVC ToDo List应用。

AngularJS_bull_TodoMVC_and_Deploy_a_NodeJS_application_with_MongoDB_add-on_on_Heroku_zps3irnkmjb

结论 (Conclusion)

That’s it folks. I hope you learned how simple and fun is to deploy an Express app with Mongoose on Heroku. In the meantime, go through the Heroku Node docs and drop a line if you’d like to see more blog posts about any other Heroku things or other services similar to Heroku. Thanks for reading!

就是这样。 我希望您了解到在Heroku上部署带有Mongoose的Express应用多么简单和有趣。 同时,如果您想查看有关其他任何Heroku事物或与Heroku类似的其他服务的更多博客文章,请遍历Heroku Node文档 。 谢谢阅读!

翻译自: https://scotch.io/tutorials/use-mongodb-with-a-node-application-on-heroku

heroku链接node

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值