Deploying a Node Application on Heroku

Tim Roberts
Let’s Learn:
Published in
4 min readMay 25, 2016

WordPress touts an impressive “5 minute” install, getting the client and developer into the environment the end-user will be experiencing. I wanted to be able to get an app up on a server somewhere to show my friends without having to do the rigmarole of figuring out how to just get some text on a screen. So in order to boil it down in a way that future Timi can understand and steal from, let’s walk through creating and deploying a Node app on Heroku.

This assumes that you have Heroku toolkit installed and a Heroku account. You can check out the repo if you just want the code

First we create a project folder, maybe something like super-sweet-node-app or what have you. Inside of that folder, which we’ll call root/root folder from now on, run npm init -y so we can use NPM packages. Speaking of, let’s install the two packages that we will be using, express and marked-engine.

$ npm install — save express marked-engine

And now that we have the pieces, let’s make them work. First, let’s create the basic files we always need:

$ touch server.js .gitignore README.md LICENSE.md

We are good developers so we get into the habit of using git now, early on. Put what files you want git to not keep track of or directories inside of .gitignore. README and LICENSE are not mandatory but it makes you look cool so might as well use them.

Inside of server.js, let’s create an express server instance that tells us when it starts.

var express = require('express');

var app = express();

var PORT = process.env.PORT || 5000;

app.listen(PORT, function(val){
console.log('I am listening on the port ' + PORT);
console.log('this callback received val as ' + val);
});

val returns null right now so that might not be a real thing. But now we can see that instantly by, in the root of our project, running

$ node server.js

Typing all of that out would be laborious and we are lazy, especially when it comes to adding alot of flags or doing more stuff in the console. Let’s abstract that command in our package.json file.

...“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1",
“start”: “node server.js” },
...

Now you can

$ npm start

and it will start the server for us locally. But it’s pretty boring when we aren’t sending anything to the client. Let’s do what every language and framework teaches us at the beginning: Hello, world

app.get('/', function(req, res){
res.send('Hello world!');
});

Put that somewhere after app = express() and when you visit localhost:5000 you should get Hello world! printing to the browser. Pretty cool, but what if I want to send a markdown file instead of static text? That is where marked-engine comes into play. Change your code to tell our express server what engine it needs to use and tell it to use a future file called TUTORIAL.md

...

app.engine('md', require('marked-engine').renderFile);

...

app.get('/', function(req, res){
res.render('TUTORIAL.md');
});
...

This tells our worker to render this file. But since we didn’t use app.configure in order to set some stuff, we need to create a folder called views and create the .md file inside of that folder.

$ mkdir views && touch views/TUTORIAL.md

Now, let’s populate that file with some markdown. You can write your own or copy it from my repo. Now run npm start and make sure that TUTORIAL.md is displaying to the screen.

Once you have that, let’s add and commit in git to get it up to date with our files. Make sure you have node_modules inside of your .gitignore

$ git add — all
$ git commit -m “App finished”

Inside of your root folder, run

$ heroku create

Once that is finished, let’s push our code to the live server

$ git push heroku master

That will deploy our files and application to a Heroku server. It will use the packages installed with the — save flag in order to create the environment. When it has finished, you will be given a url. If you visit this url, you should see the exact same thing as you do when you visit localhost:5000 when you have it running locally.

And there you have it! The most basic way to get an express server running on a hosted server. Heroku has a lot more tools and offers way better tutorials than this for every step of the way.

But like a wise man once said: Once you learn something, build a tutorial of how to do it.

Was I wrong in any of this? Are there glaring errors that I’m too green to understand? Open an issue on the github repo or send me a tweet. Did this help and wanna help others? Click the little cute heart below and share it!

--

--

Tim Roberts
Let’s Learn:

dev kid who likes to write in english instead of code