Wednesday, November 1, 2017

How to Deploy Node Applications: Heroku vs Now.sh

As Node.js continues to gain in popularity, new tutorials pop up teaching you to write server-side JavaScript apps and APIs. Once you've built your shiny new Node app, though, what then?

In this article, I'm going to take a look at a couple of options for deploying your Node applications. We'll take a look at Now.sh and Heroku.

I'll explain how to deploy your code to each platform and we'll end the article with a short summary of the pros and cons. I'll pay attention to options for monitoring, ease of use, offered functionality and what the free hosting plan includes.

Deployment with Heroku

To be able to deploy apps to Heroku, you will have to sign up at Heroku and install the Heroku CLI for your machine. I prefer working from my terminal!

Before we can start, we need to add some code to the Procfile. Heroku makes use of this file to determine how to execute the uploaded code.

The following code needs to be added to the file so Heroku knows what command should be executed to start the app:

web: node app.js

Once this is done, try to log in from the terminal by typing heroku login. Heroku will ask you to enter your login credentials.

Next, navigate to the root of your project and enter the command: heroku create. This creates an app on Heroku which is ready to receive the source code of your project. The name of the app on Heroku is randomly created.

To deploy our code to Heroku, simply use git push heroku master. We can visit the app with the command heroku open which will open the generated URL.

Pushing changes to Heroku

Changes can be pushed by following the normal Github flow:

git add .
git commit -m "Changes made to app"
git push heroku master
heroku open

Useful Heroku Commands

  • To make sure that at least one instance of the app is running: heroku ps:scale web=1
    Because we are using the free platform, it is not possible to upscale your application. However, it is possible to downscale so no instances of the application are running: heroku ps:scale web=0

  • View the latest logs (stream) in chronological order generated by Heroku: heroku logs --tail
    It's also possible to show the app logs only. App logs are the output of console.log() statements in your code and can be viewed with heroku logs --source app-name

  • Heroku provides the possibility to run your app locally at http://localhost:5000: heroku local web

  • List all Heroku apps: heroku apps

  • Remove a deployment: heroku apps:destroy --app app-name

  • Add owner (account) to access the app: heroku access:add me@email.com, same for removing heroku access:remove me@email.com

Heroku Environment Variables

If you are working with a .env file locally, you might want to use other environment variables for your Heroku deployment. It is possible to set these with heroku config:set PORT=3001. These values overwrite the variables set in you .env file.

To see all defined Heroku environment variables, just use heroku config. If you want to remove an environment variable for e.g. PORT, use heroku config:unset PORT.

Continue reading %How to Deploy Node Applications: Heroku vs Now.sh%


by Michiel Mulders via SitePoint

No comments:

Post a Comment