These instructions will show you how to create an Express (a Node.js web application framework) 'Hello World' website from scratch, then deploy it on the Heroku server.
- Node.js - https://www.nodejs.org
- git - https://git-scm.com
- Free Heroku Account - https://www.heroku.com
I use the name i-express
, you can call it anything you want, except express
. It cannot be the same name as the Express package.
mkdir i-express
cd i-express
npm init
You will be asked a few questions after invoking npm init
, the defaults can be used without any problem.
This will create a package.json file. You can treat this as a text file and modify the content if necessary.
npm install express --save
Create a file called server.js
server.js:
var express = require('express');
var app = express();
app.get('/', (request, response) => {
response.send('<h1>Hello World</h1>');
});
var port = process.env.PORT || 3000;
app.listen(port);
console.log('server started ' + port);
node server
You should be able to bring up a Hello World page by visiting http://localhost:3000.
After successfully testing the site, you can stop the server by pressing CTRL+C
.
- Sign in to https://heroku.com
- Select New > Create new app to create a new app
- Enter an App name (e.g. in my case
i-express
) - Install the Heroku CLI
npm install -g heroku-cli
- Login to Heroku
heroku login
Note: If the heroku command is throwing an error (e.g. incompatible Node version), other methods of installing Heroku CLI can be found here.
Create a .gitignore
file to prevent files in node_modules from being tracked and uploaded to Heroku.
.gitignore:
node_modules/
Initialize git and deploy to Heroku
git init
heroku git:remote -a i-express
git add .
git commit -am "Create Hello World page"
git push heroku master
Voilà! Your new site is now live at https://<AppName>.herokuapp.com
- Ivan Lim - Initial work - Active Improvement Web Studio
This project is licensed under the MIT License - see the LICENSE file for details