Introduction
NodeJs is a cross-platform runtime environment that allows developers to execute application code on a private server instead of a web browser. It is built for writing server-side applications with javascript. It is a technology of choice for a large number of developers. It's used in production by many companies like Netflix, uber, etc.
In order to show how to deploy NodeJs applications, we’re gonna make a sample application named ‘sample1’.Now,the next step is to download and install nodeJs. After that, follow the steps below:
Steps to Deploy NodeJs Application
STEP 1: Create a “package.json” file using the following command
npm init
STEP 2: Open your project folder and create a js file and name it “app.js”.
STEP 3: Inside your project folder, create a html file and name it “head.html”.
Inside the html file, fill the following code
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>This is the Homepage</h1>
<p><a href="/tailPage">Go to Next Page</a></p>
</body>
STEP 4: Create another html file under same folder and name it “tail.html”.
Fill in the following code inside your file
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>This is the Homepage</h1>
<p><a href="/tailPage">Go to Next Page</a></p>
</body>
STEP 5: Open “app.js” file and fill the following code nodejs application.
var http = require('http');
var fs = require('fs'); // to get data from html file
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
// req.url stores the path in the url
var url = req.url;
if (url === "/") {
fs.readFile("head.html", function (err, pgres) {
if (err)
res.write("HEAD.HTML NOT FOUND");
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(pgres);
res.end();
}
});
}
else if (url === "/tailPage") {
fs.readFile("tail.html", function (err, pgres) {
if (err)
res.write("TAIL.HTML NOT FOUND");
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(pgres);
res.end();
}
});
}
}).listen(3000, function () {
console.log("SERVER STARTED PORT: 3000");
});
STEP 6: Open the terminal again and write the following command to run the server
node app.js
Now, if you type “localhost:3000” in your browser, you will see your application running.
We have completed our basic part, now its time to deploy it into the web. There are many cloud platforms like digital ocean, heroku etc. For our example, we'll be using heroku as it is easy to use and free for learners node js application.
Before moving further, it's recommended to install git on your system.
STEP 1: Go to heroku site and get registered.
STEP 2: After completing the registration, go to https://dashboard.heroku.com/apps
STEP 3: Download heroku CLI for your system.
STEP 4: In the command line, type
heroku login
After successful login, following will be shown
STEP 5 : Type
git init
Now type
git add .
Now, we need to commit the files we have added to git. Type
git commit -m "initial commit"
STEP 6 : Now, create the heroku app by command
heroku create
This will create a git remote which is connected to our local git repository in nodejs application
STEP 7 : Type the following to deploy the app on heroku server
git push heroku master
STEP 8 : In order to make sure one instance of the app is running, type
heroku ps:scale web=1
STEP 9: To open the app in the browser, type
heroku open
This will open as:
Go to command line and open logs to check for errors i.
It's saying “npm ERR! Missing script: start”
To fix this problem, we need to set up a start script, the start script tells the server to run “node app.js” after installing the packages.
STEP 10 : Open “package.json” and type “start”: “node app.js” inside the scripts tag.
There is one more problem right now.We are using PORT: 3000 but Heroku doesn’t. Heroku is using dynamic port. Therefore, we need to change the code as follows in app.js file in nodejs application.
var http = require('http');
var fs = require('fs'); // to get data from html file
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
// req.url stores the path in the url
var url = req.url;
if (url === "/") {
fs.readFile("head.html", function (err, pgres) {
if (err)
res.write("HEAD.HTML NOT FOUND");
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(pgres);
res.end();
}
});
}
else if (url === "/tailPage") {
fs.readFile("tail.html", function (err, pgres) {
if (err)
res.write("TAIL.HTML NOT FOUND");
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(pgres);
res.end();
}
});
}
}).listen(process.env.PORT || 3000, function () {
console.log("SERVER STARTED PORT: 3000");
});
STEP 11 : Again type in nodejs application
git add .
git commit -m "another commit"
git push heroku master
heroku open
Hurray! Your first web application deployed.
Check out most important Git Interview Questions here.