To teach you to deploy Node.js applications on the Heroku cloud platform _node.js

Source: Internet
Author: User
Tags commit mongodb node server git commands mongolab heroku logs heroku toolbelt

Although AWS now has its own PAAs platform called Elastic Beanstalk, the advantage of Heroku is that it has a number of third-party add-on, such as Mongodb,mysql, Redis, memcached, and various message queues, SMS and mail, background batch, search, automatic monitoring and other plug-ins, from an ecological perspective than AWS or much stronger.

That is to say, there is a ready-made node.js PAAs running environment on the Heroku, and a lot of add-on, which automatically backs up the data to AWS S3 every day, you just need to push the code up with Git commands, and nothing else. Is there a better thing in the world for us to program apes?

So, in order to benefit the netizen, I today put forward a few small steps of deployment summary.

Suppose you already have a node.js application called sample, the code is placed under/home/apps/sample9527, the directory has your server-side core JS file Server.js, now you have CD to this directory.

In order for Heroku to identify your application, you need to include a file Procfile in this directory, one line of code:

Web:node Server.js

(The Web:node here is to tell Heroku that you are deploying a node.js Web application, and that the server.js is your server-side core program code file name)

Well, now the deployment can begin.

First, you need to register a user on the www.heroku.com, the username is your email address. Then install Heroku Toolbelt, which is a command-line tool that lets you publish code directly in the shell, view the system state, modify the server configuration, and so on.

Then log in with the Heroku login command in the shell and enter your username and password.

$ Heroku Login

The first step can be used foreman start command to check whether your code can run smoothly on the Heroku, if you see the output is not an error, it explains the basic normal.

$ foreman Start

And then just use git to sync the code:

$ git init

$ git Add.

$ git commit-m "Init"

To create a Heroku application:

$ Heroku Create sample9527

If sample9527 this app name is not used in Heroku, then you will create success, the application of the link is http://sample9527.herokuapp.com/, or change the name to try, or directly with the Heroku Create let it help you with a new name.

If you need to install some plug-ins such as databases, you can see what add-on is available and then join at the command line. For example, join a Mongolab-provided MongoDB database:

$ Heroku Addons:add Mongolab

(Most add-on need you to fill in the user information in the credit card, but if you choose the free version will not be deducted fees, rest assured)

Then you can sync the program code:

$ git push Heroku Master

After synchronization, Heroku will automatically download and install the necessary dependencies based on NPM configuration, and then start your application.

Now you can look at the application link is normal, if there are errors, you can view the log at the command line:

$ Heroku Logs

After modifying the code based on the error message in the log, you can submit the code again:

$ git commit-a-M "Update some code"

$ git push heroku Master

or restart the service directly with the restart command when needed:

$ heroku Restart

Heroku's primary play is basically these, simple enough? Try it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.