Skip to main content

Up and running Node.js

      What we are gonna do?

We will be creating a web app. A user can find gifs based on the word. To implement this we will be creating a backend service using Node.js, Express, and frontend using React. We will use Github for Source Code Management and finally we will deploy our project to AWS. We will also set up a pipeline for CI/CD (Continuous Integration and Continous Deployment).

Setup the development environment

This part you have to do on your own.

Fun part. Start coding.

Fire up the terminal if using ubuntu hit alt+ctrl+t.

Create a new directory for the project.

Image for post

Voila, npm will create a new package.json file.

Now let’s start writing code. We will use express for creating the backend server. Discussion about express is out of the scope for this post. Let’s pull express as a dependency from npm.

Image for post

npm will create a new package-lock.json file and node_modules directory.

Server

Writing and running the express server is pretty easy. Simply create a new file index.js and paste the below code.

var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Server is up and runnig on port 3000!');
});

Now let’s test our server. Run node index.js and visit http://localhost:3000/ to check your server in action.

Integrating with Giphy API

To integrate with Giphy we need GIPHY’s API. Go and create a new app in GIPHY and obtain the API key.

To make an API call to GIPHY API we need another npm package request and request-promise.

Run npm install request — save and npm install request-promise — save.

Once done. Let’s create a new API /gif. Update your code with the following snippet.

var express = require("express");
var app = express();
var request = require("request");
var rp = require("request-promise");
app.get("/", function(req, res) {
res.send("Hello World!");
});
app.get("/gif", async function(req, res, next) {
const options = {
uri: "https://api.giphy.com/v1/gifs/search",
method: "GET",
qs: {
api_key: "<API_KEY>",
q: req.query.search,
limit: 3,
offset: 0,
rating: "G",
lang: "en"
}
};
const response = await rp(options);
res.json(response);
});
app.listen(3000, function() {
console.log("Server is up and running on port 3000!");
});

Now restart the server. And the endpoint

http://localhost:3000/gif?search=shashi

And you can see the returns response from GIPHY’s search API. Now our backend is ready to deploy.

Deployment (Not a rocket science)

Prerequisites

All set?

Github

Let's initialize the repository, run git init.

Image for post

Create a new file .gitignore and paste the following content.

node_modules

Now run some more git commands:

git status

git add .

git commit -m “awesome commit message”

git remote add origin <YOUR_GITHUB_REPOSITORY_URL>

git push origin -u master

Provide your username and password.

At this point, you have successfully pushed your code to GitHub.

AWS Server

Login to your AWS console.

Image for post

Click on the Launch Instance button.

Image for post

Scroll down and click on the Select button for Ubuntu Server.

Image for post

Go ahead with the auto-selected t2.micro instance. Click on the Review and Launch button.

Image for post

3, 2, 1, and click on the Launch button.

Image for post

Choose “Create a new key pair” from the dropdown menu. Input the name. And download the key. It will download a .pem file.

KEEP THIS KEY FILE SAFE AND DON’T LOOSE IT. YOU WON’T BE ABLE TO DOWNLOAD IT LATER.

Image for post

Once you have downloaded the file. The Launch Instance button will be activated. Click on the Launch Instance button to launch a new instance.

Image for post

Awesome we have just launched a new AWS EC2 instance.

Navigate to Services -> EC2 to view the instance.

Image for post

Well done.

Now scroll to the right side and click on launch wizard.

Image for post
Image for post

Click on the Inbound tab on the down.

Image for post

Click on Edit

Image for post

Click on Add Rule

Image for post

Give Port Range as 3000 and select Source as anywhere and Save. This will allow our incoming traffic on our server on port 3000 from anywhere over TCP protocol.

Well done.

Now let's deploy our backend service on the EC2 instance. For now, we will do it manually.

Select your instance and click on connect. You will see instructions on how to connect to the EC2 server.

Image for post

Give the 400 permission to your .pem file.

sudo chmod 400 <KEY>.pem

Now we are ready to connect with our server using SSH. Execute the following command.

ssh -i <KEY>.pem ubuntu@ec2–13–127–93–105.ap-south-1.compute.amazonaws.com

Image for post

if prompted write yes and enter. You will be connected to your EC2 instance.

Image for post

Setting up the server.

Let’s set up our server to host our application.

sudo apt-get update

Install Node.js and npm.

sudo apt-get install nodejs

sudo apt-get install npm

Clone your repository.

git clone <YOUR_REPOSITORY_URL>

cd <PROJECT>

Install npm dependencies

npm install

Start the server

node index.js

Now go to <YOUR_EC2_PUBLIC_IP>:3000/gif

Woah you have deployed your application on the server.

Next, we have to do some more things.

2. Setup CI/CD pipeline for automatic deployment of our application.

Thanks for reading. If you have some feedback, please provide your response or reach out to me on Twitter or Github.

Comments

Popular posts from this blog

Visualising Your Google Location History

Step 1: Download Your Google Location History First, you need to download your location history data from Google. Follow these steps to get started: Go to Google Takeout : Visit Google Takeout to begin the process. Select Location History (Timeline) : Make sure to select the “Location History (Timeline)” option. 3. Export Your Data : Google will compile your data, which might take some time. You will receive an email once your data is ready to be downloaded. 4. Download the Data : Follow the instructions in the email to download your data. Step 2: Extract the Downloaded File Once you’ve downloaded your data, locate the file and extract it. The location data will be in a JSON file at the following path: takeout-202XXXXXTXXXXXXZ-001/Takout/Location History (Timeline)/Records.json Step 3: Convert JSON to CSV To make the location data usable, we need to convert it from JSON to CSV format. Although you can convert it to KML or geoJSON formats, we’ll focus on CSV for this post. 1.  Clon...

How to mine pancake swap using your Laptop/Desktop

In this post, we will talk about how you can mine pancake swap using your laptop or desktop. What is pancake swap you ask? Pancakeswap is a decentralizaed DEX where you can Trade, earn, and win crypto on the most popular decentralized platform in the galaxy, you can Trade any token on Binance Smart Chain in seconds and you can earn passive income with your CAKE tokens. You can read more about it from  https://pancakeswap.finance/ Get Started! Step I: Getting a crypto wallet You can start with  Metamask  as a crypto wallet. Go ahead and download the Metamask as a chrome extension. Download Link:  https://metamask.io/download.html Once installed let's set up the Metamask account. Metamask Get Started Screen Click on  Get Started  button, and you should see the following screen. Metamask create wallet screen Click on  Create a Wallet  button. Metamask I Agree Screen Click on  I Agree  button. Metamask create password Provide a strong passwo...

Unleashing the Power of Personal Photo Management with PhotoPrism

As an avid photographer and a lover of memories, I've always struggled with managing and organizing my ever-growing collection of digital photos. It was a constant battle to find specific photos, especially as I often forget to tag or rename them. That's when I stumbled upon PhotoPrism , a powerful open-source photo management tool that has revolutionized the way I interact with my photo library. In this blog post, I'll guide you through the process of installing and using PhotoPrism, sharing my personal use cases along the way. Whether you're a professional photographer, a hobbyist, or simply someone passionate about preserving memories, PhotoPrism is a game-changer. Getting Started To start our journey with PhotoPrism, the first step is to install it. Head over to the official repository on GitHub and follow the installation instructions provided there. Don't worry, the installation process is well-documented and straightforward, making it accessible even for no...