Teach you to deploy node. js applications on the heroku cloud platform

  • 2020-03-30 03:37:46
  • OfStack

Although AWS now has its own PaaS platform called Elastic Beanstalk, heroku's advantage is that it has many third-party add-ons, such as MongoDB, mysql, redis, memcached, various message queues, SMS and mail, background batch processing, search, automated monitoring and so on.

In other words, heroku has an existing node. js PaaS runtime environment and a lot of add-ons, which automatically backup data to AWS S3 every day. All you need to do is push the code with git command and nothing else. Is there anything better in the world for us programmers?

So, in order to benefit the net friend, an today deploy a few small steps summary.

Suppose you already have a node.js application called sample, the code is placed under /home/apps/sample9527, the directory contains your core server-side js file, server.js, and now you are in this directory.

In order for heroku to recognize your application, you need to add a file, Procfile, to this directory.


web: node server.js

Web: node here tells heroku that you are deploying a node. js web application, followed by server. js is the file name of your server-side core program.

Ok, now the deployment can begin.

First, you need to register a user at (link: http://www.heroku.com), and the user name is your email address. Then install Heroku Toolbelt, a command-line tool that lets you post code directly from the shell with commands, view system status, modify server configuration, and more.

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


$ heroku login

The first step is to check if your code is running on heroku with the foreman start command, and if you see no errors in the output, it's almost normal.


$ foreman start

Then you synchronize your code with git:


$ git init

$ git add .

$ git commit -m "init"

Create heroku application:


$ heroku create sample9527

If sample9527 this app name no one used on heroku, you will create success, application links (link: http://sample9527.herokuapp.com/), or change a name and then try, or directly use heroku create to make it a new name for you.

If you need to install a plug-in such as a database, you can see what add-ons are available and then add them at the command line. For example, add MongoDB database provided by MongoLab:


$ heroku addons:add mongolab

(most add-ons require you to put a credit card in the user's information, but if you choose the free version, you won't be charged. Rest assured.)

Then you can synchronize the program code:


$ git push heroku master

After synchronization, heroku will automatically download and install the necessary dependencies according to the NPM configuration, and then start your application.

Now you can go to the application link to see if it works. If there is an error, you can check the log at the command line:


$ heroku logs

After modifying the code according to the error message in the log, you can commit the code again:


$ git commit -a -m "update some code"

$ git push heroku master

Or simply restart the service with the restart command when needed:


$ heroku restart

Heroku's primary gameplay is basically this, is it easy enough? Try it yourself.


Related articles: