application to heroku

Deploying an Express Node.js Backend on Heroku

3228 VIEWS

· ·

Building applications locally is probably the easiest way to develop. But sometimes, when it comes to getting it on the Web, you have a lot to consider. After making your Node.js app work on localhost, your next step is to deploy it to the cloud so that it’s publicly accessible. There are various cloud services (dubbed Platform-as-a-Service, or PaaS) that enable app deployment: Amazon Web Services, Google Cloud Platform, Digital Ocean, Heroku, and many others. In this post, I will show how to deploy your Node application to Heroku.

Heroku is a great choice for quick prototypes (for startups) and small-medium scale projects because it abstracts away most of the steps required in setting up your servers/infrastructure. In this tutorial, we will be deploying a Express Node.js application (written about in this earlier Sweetcode post) to Heroku.

Prerequisites

Here’s the code for the package.json and app.js files from the above-linked Sweetcode post.

package.json

{
	"name": "hero",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
    	"start": "node index.js",
    	"test": "echo \"Error: no test specified\" && exit 1"
	},
	"author": "",
	"license": "ISC",
	"dependencies": {
    	"express": "^4.16.4"
	}
}

Index.js

const PORT = process.env.PORT || 3000;
const express = require("express");
const app = express();

app.use(express.json());

const courses = [
	{ id: 1, name: "Algorithms" },
	{ id: 2, name: "Software Engineering" },
	{ id: 3, name: "Human Computer Interaction" }
];

app.get("/", function(req, res) {
	//when we get an http get request to the root/homepage
	res.send("Hello World");
});

//when we route to /courses
app.get("/courses", function(req, res) {
	res.send(courses); //respond with the array of courses
});

//To get a specific course, we need to define a parameter id
app.get("/courses/:id", function(req, res) {
	const course = courses.find(c => c.id === parseInt(req.params.id));

	//if the course does not exist return status 404 (not found)
	if (!course)
    	return res
        	.status(404)
        	.send("The course with the given id was not found");

	//return the object
	res.send(course);
});

//using the http post request we can create a new course
app.post("/courses", function(req, res) {
	//create a course object
	const course = {
    	id: courses.length + 1,
    	name: req.body.name
	};

	//add the course to the array
	courses.push(course);

	//return the course
	res.send(course);
});

app.put("/courses/:id", function(req, res) {
	//get the course
	const course = courses.find(c => c.id === parseInt(req.params.id));

	if (!course)
    	return res
        	.status(404)
        	.send("The course with the given id was not found");

	//update the course
	course.name = req.body.name;

	//return the updated object
	res.send(course);
});

app.put("/courses/:id", function(req, res) {
	//get the course
	const course = courses.find(c => c.id === parseInt(req.params.id));

	if (!course)
    	return res
        	.status(404)
        	.send("The course with the given id was not found");

	//update the course
	course.name = req.body.name;

	//returns the updated object
	res.send(course);
});

app.listen(PORT, function() {
	console.log(`Listening on Port ${PORT}`);
});

Run

npm instal

l in the terminal to install the dependencies.
Create a Heroku account here.
Download and install the Heroku CLI.

Step 1: Preparing the codebase for Heroku

Now that we have the base code for the application, we need to make a few changes to the package.json file to enable deployment to Heroku.

1. Run the node –version command to get your version of Node.js.
2. Add the engines key to your package.json, specifying your node version.

package.json

{
	"name": "hero",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
    	"start": "node index.js",
    	"test": "echo \"Error: no test specified\" && exit 1"
	},
	"author": "",
	"license": "ISC",
	"dependencies": {
    	"express": "^4.16.4"
	},
	"engines": {
    		"node": "v11.3.0"
	}
}

Step 2: Testing Heroku locally

1. Initialize the Git repository and commit the changes we just made.
`git init`
`git add .`
`git commit -m “First deploy to Heroku”

2. Log in to Heroku (one-time command) by running heroku login

3. Run heroku local web

3. Open up localhost:5000 in your browser to see the result:

Step 3: Deploying to Heroku

1. Run heroku create [chosen name of your project] to create the Heroku app.
2. Run git push heroku master to push the code to the Heroku servers.

After the process is complete, visit the Heroku link for your newly deployed app (which should be printed near the bottom of the output).

Great Job! You’ve just deployed your Node.js application to Heroku. To make changes after deploying, simply

git commit

your changes and run

git push heroku master

once again.


Kevin de Youngster is a CS major at Ashesi University with 2 years experience in coding. He has experience with python, web development (CSS, HTML, JS) and NodeJS. Having a keen interest in how software is used to enhance other industries, he has interned with a number of companies and is currently working at Chalkboard Education. When he isn't immersed in building projects, he spends his time watching nature videos and making illustrations.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu