The Simplest Node.js Application Ever
How to create and deploy a very basic Node.js web application.
<iframe src=”https://player.twitch.tv/?autoplay=false&video=v427220270" frameborder=”0" allowfullscreen=”true” scrolling=”no” height=”378" width=”620"></iframe><a href=”https://www.twitch.tv/videos/427220270?tt_content=text_link&tt_medium=vod_embed" style=”padding:2px 0px 4px; display:block; width:345px; font-weight:normal; font-size:10px; text-decoration:underline;”>Watch Creating a Web App with Node.js & Express.js from aaroncodes on www.twitch.tv</a>
Prerequisites
- You need to have an account with GitHub and Heroku.
- Here’s how to set up git, although it was probably pre-installed on your computer.
Install Node.js
If you’re on Ubuntu, from the terminal…
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
Otherwise, follow these instructions.
Install Heroku
Install Heroku. If you’re on Ubuntu you can just type…
sudo snap install --classic heroku
Create a working directory
Find a good location to save the new project. I’ll just put mine on the Desktop. From the command line, create a new directory, then move into that directory…
cd ~/Desktop
mkdir simple-node-app
cd simple-node-app
Create the repository
Once logged in to GitHub create a new repository. I’ll call mine simple-node-app.
You should be in the project directory. Now, copy and paste the text from GitHub into the terminal. If you copy my text, change the username.
echo "# simple-node-app" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:aaronshivers/simple-node-app.git
git push -u origin master
Initialize NPM
We’ll just use the default settings here.
npm init -y
Install Express.js
We’re going to use Express.js, a simple web framework.
npm i express
Create a main file
Let’s create a main file called index.js
touch index.js
Open Text Editor
I’m using Sublime Text, so I’ll open it like this…
subl ./
Setup Package.json
Open package.json in the text editor. Notice that I removed the test script on line seven and replaced it with a start script. This is necessary for running the application on Heroku.
Setup Index.js
- We’ll start by requiring the express package we installed earlier.
- Set app to be an instance of express.
- Set port to be whatever port is being used by default, or port 3000 if no default is set.
- Create a GET route for ‘/’, and send a welcome message.
- Finally, make the server run on the specified port.
It should look like this when you’re done…
Testing
Start the application from the command line…
npm start
You should see a message like…
Server running on port 3000.
Now open a new tab in your browser and go to localhost:3000. You should see a message like, “Hello, People!”. If so, you can stop the application by typing CTRL+C in the terminal.
Setup Heroku
Create the app on heroku. From the terminal…
heroku create
This creates a new repository on Heroku like the one on Github, and a link where we’ll find our application after deployment.
Commit and Deploy
Now, we can finally send our files to GitHub and Heroku, and deploy our application.
Well, almost. Before we do that, let’s tell git what files we don’t want to commit. Create a file called .gitignore…
touch .gitignore
Open the file in the text editor and add the node_modules folder to .gitignore. We don’t need to include this, because Heroku will just download those files on installation based off of the dependencies listed in package.json.
We can make sure that worked by viewing the untracked files. You should see the .gitignore file, but not the node_modules folder.
git status
Select which files to track, which will be all of them…
git add .
Commit the files with a message…
git commit -m 'Initial commit'
Push the files to GitHub and Heroku
git push && git push heroku master
You should see the link again somewhere around five lines from the bottom of the terminal. If you follow it, it will take you to your new fully deployed web application. If you see “Hello, People!” in the browser, everything was successful.
Here’s mine — https://boiling-sierra-17785.herokuapp.com/
Conclusion
Thanks for following along. You can find the continuation of this project at the following link.