Although AWS has now launched its own PaaS platform, called Elastic Beanstalk, heroku's advantage is that it has many third-party add-ons on it, such as MongoDB, mysql, redis, memcached, and various message queues, sms and email, background batch processing, search, automated monitoring and other plug-ins, which are much better than AWS from the perspective of the ecological environment.
This means that there is a ready-made Node.js PaaS running environment on heroku, and there are many add-ons. The data will be automatically backed up on AWS S3 every day. You just need to use the git command to push the code, and you don’t have to worry about anything else. For us programmers, is there anything more exciting in the world than this?
So, in order to benefit netizens, I will summarize a few small steps of deployment today.
Suppose you already have a node.js application called sample, the code is placed under /Home/Apps/sample9527, and your server-side core js file server.js is in the directory. Now you have cd it to this directory.
In order for heroku to recognize your application, you need to add a file Procfile in this directory, which contains a line of code:
web: node server.js
(The web: node tells heroku that you want to deploy a node.js web application, and the server.js behind is your server-side core program code file name)
OK, now the deployment is ready.
First, you need to register a user at www.heroku.com, and the user name is your email address. Then install Heroku Toolbelt, a command-line tool that allows you to publish code directly in the shell with commands, view system status, modify server configuration, etc.
Then log in with the heroku login command in the shell and enter your username and password.
$ heroku login
The first step is to use the foreman start command to check whether your code can run smoothly on heroku. If you see no errors reported in the output, it means that it is basically normal.
$ foreman start
Then use git to synchronize the code:
$ git init$ git add .$ git commit -m "init"
Create heroku app:
$ heroku create sample9527
If the app name sample9527 is not used on heroku, then you will be created successfully. The link to the application is http://sample9527.herokuapp.com/, otherwise you can change the name and try it, or use heroku create directly to let it give you a new name.
If you need to install some plugins like databases, you can see what add-on is available and then join it on the command line. For example, add a MongoDB database provided by MongoLab:
$ heroku addons:add mongolab
(Most of the add-ons require you to fill in your credit card in user information, but if you choose the free version, you won’t be deducted. Don’t worry)
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 launch your application.
Now you can go to the application link to see if it is normal. If there is an error, you can view the logs on 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 use the restart command to restart the service when needed:
$ heroku restart
This is basically the basic gameplay of heroku, isn't it simple enough? Go and try it yourself.