Hello, my dear friends. In my previous article I did a configuration of AWS EC2 cloud. Now since the EC2 instance is fully configured, its time to host a web application on the instance to reap the benefits.
Do an RDP or remote login to the EC2 instance using the elastic IP and the password which you can get by using the .pem file which we downloaded in the previous article.
After logging in the instance, open Internet explorer and browse to https://nodejs.org/en/download/ . You should be able to browse to this website as we have configure an internet gateway for the subnet which is assigned to the VPC which we created for this instance.
Download the Windows installer for the 64-bit windows as we created an 64 bit EC2 instance.
One important point which you need to take care on this EC2 instance. And that is, you have to open port 80 for inbound rules in the windows firewall. As I am running the node js app on port 80. Otherwise you will no be able to browse the application from outside the instance.
Once the download is complete open the command prompt and type the below two commands.
node --version npm --version
And you should be able to see the version as shown in the figure 1 below.
Now create folder in the system in any drive. I have created a folder in C drive with the name “MyNodeApp”. Go to the folder, and create a file named app.js with following contents.
const express = require('express') const app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(80, function () { console.log('Example app listening on port 80!') })
Open the command prompt and navigate to the folder we just created. And run the below command
npm –init
and the command prompt will ask you some configuration related options which you need to enter. Please have a look at the below figure 2. Once you are done a file named package.json is created in the folder.
Execute the next command which is mentioned below
npm install express –save
and the express module related files are downloaded in the folder.
And now its time to run the node server on the machine. Use the below command to achieve the same.
node app.js
and you can see that our node server is up and running. Have a look at the figure 3 below.
Now you can browse the application from anywhere in world provided that you are connected to internet. You can browse the application using the elastic IP of the EC2 instance e.g. http://xxx.xxx.xxx.xxx.
That was all about setting up EC2 instance and host a node js application on the same instance.
I hope this two article series will help you to host you own node js application on AWS EC2 instance.
Leave a Reply