Hello World

Brief guides about how to build simple hello world applications using different languages

JavaScript Node.js Express Application

Prerequisites

Initializing Project

Create project directory

mkdir myapp
cd myapp

Init Node.js project

npm init

Follow on-screen prompts. Enter to accept defaults except entry point (index.js), enter app.js instead.

Install express and save in the dependencies list

npm install express --save

Creating Hello World

Create or edit app.js with the following content

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Run locally

node app.js

The app should serve at http://localhost:3000 

image-1597384411246.png

References

.NET Core Web MVC Application

Prerequisites

Initialize Project

Create ASP.NET Core project structure

dotnet new webapp -o aspnetcoreapp

Run locally

dotnet watch run

image-1597384837314.png

References

Python Flask Web Application

Prerequisites

Python v3.6+

Usually come the Ubuntu 18.04 by default. Check version:

$ python3 --version
Python 3.6.9

To create alias python -> python3, use this commands:

$ sudo update-alternatives --install /usr/bin/python python $(command -v python3) 1
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

Pip v3.6+

Install from package:

sudo apt-get update && sudo apt-get install python3-pip -y

Check version:

$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Create alias pip -> pip3:

$ sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
update-alternatives: using /usr/bin/pip3 to provide /usr/bin/pip (pip) in auto mode

Virtualenv

Install from pip:

sudo pip install virtualenv

Initialize Project

Create a new project directory and initialize virtual environment:

virtualenv env

Activate virtual environment:

source env/bin/activate

Install Flask

pip install flask

Create app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return "Hello, World!"

if __name__ == "__main__":
  app.run()

Run the app:

python app.py

Go to http://127.0.0.1:5000/ 

image-1598845948724.png

References

Java Web Application

Prerequisites

JDK and Apache Maven

On Ubuntu 18.04, you can install maven and you will also get OpenJDK.

sudo apt-get update && sudo apt-get install maven -y

After installation is completed, check versions:

$ java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)

$ mvn --version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.8, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-17763-microsoft", arch: "amd64", family: "unix"

Or you can manually install from:

Configure JAVA_HOME

Find JDK home directory

$ java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
    java.home = /usr/lib/jvm/java-11-openjdk-amd64

Edit ~/.bashrc and append these lines:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Tomcat

Tomcat is required to run your WAR file. Download Apache Tomcat 8.5 from https://tomcat.apache.org/download-80.cgi

Extract files and append the following lines to ~/.bashrc.

CATALINA_HOME=$HOME/bin/apache-tomcat-8.5.57
PATH=$PATH:$HOME/bin/apache-tomcat-8.5.57/bin

Configure Tomcat

Insert the following lines between <tomcat-users></tomcat-users> block

  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="password" roles="manager-gui, manager-script"/>

Start and Stop Tomcat

Start Tomcat

startup.sh

Visit http://localhost:8080 to check server is up

Stop Tomcat

shutdown.sh

Initializing Project

Generate the project

mvn archetype:generate "-DgroupId=sh.par" "-DartifactId=helloworld" "-DarchetypeArtifactId=maven-archetype-webapp" "-Dversion=1.0-SNAPSHOT"

Build and package

mvn package

The WAR file in target folder.

Deploy to Tomcat

Start the server and go to web GUI at http://localhost:8080/ 

Click Manager App button

image-1598960550565.png

Input configured username and password.

Select the WAR file and click Deploy button

image-1598960649208.png

Your deployed application should appear on the list.

image-1598960721439.png

You can click Undeploy button to remove your application.

image-1598960766090.png

References