Tutorial 1 - Hello World

This tutorial assume you are new to NodeJs, TypeScript and KambojaJs. You will found everything you need from zero to a working KambojaJs Application.

This tutorial require:

  1. A working NodeJS installation
  2. Command Line App, terminal for Linux/Mac user, Git Bash for Windows user
  3. Text editor, Visual Studio Code (recommended)

Refer to Environment Setup for more information.

Note files of this tutorial can be found here

Install Prerequisite

Start the tutorial by installing TypeScript in your global NPM package

$ npm install -g typescript

After installation complete, check TypeScript installation by typing

$ tsc -v

Make sure you get TypeScript version 2.3 or newer

Note if you are Windows user and you found that you can’t install newest version of TypeScript, it might caused by Visual Studio has installed TypeScript globally. To fix this problem you can install typescript locally on the project folder npm install --save-dev typescript then access the tsc compiler using $ ./node_modules/.bin/tsc

Initialize Project

Start by creating a project directory and enter to the created directory. Then follow another command below to initialize package.json and tsconfig.json

$ mkdir 01-hello-world && cd 01-hello-world
$ npm init --yes
$ tsc --init

Open tsconfig.json using any text editor, delete all its content and replace with below

//tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "experimentalDecorators": true
  }
}

Install Kamboja Express

kamboja-express is kamboja module that uses Express as its core function on handle http request and http response. Go back to command line app and type

$ npm install --save kamboja-express

Create KambojaApplication and Controller

Create 2 more files app.ts and controller/home-controller.ts inside src folder. The file structure will be like below:

Alt text

app.ts will be our application bootstrap, in this file we wil create instance of KambojaApplication and start listening for http request.

home-controller.ts is our controller, an http handler that will execute some code based on route it is associated.

Open app.ts using text editor and paste this code

//src/app.ts

import { KambojaApplication } from "kamboja-express"

const app = new KambojaApplication(__dirname).init()

app.listen(5000)
console.log("Ready http://localhost:5000")

Above code showing we create instance of KambojaApplication then call its init function which is returned Express application.

By using the Express application we then listent to http request on port 5000.

Next open home-controller.ts using text editor and paste code below

//src/controller/home-controller.ts

import { Controller, http } from "kamboja-express"

export class HomeController extends Controller {
    @route.get("/")
    index() {
        return "Hello World!"
    }
}

Above code is a very simple controller which is only returned a Hello World! text.

By default above controller will handle http://localhost:5000/home/index but on above code we override the index action using @route.get("/") thats mean the index action overridden to handle http://localhost:5000/

Compile and Run

Now we are ready to test our first Hello World code. Go back to command line app and type:

$ tsc && node src/app.js

Above code will print message below in the command line app.

  [Kamboja] Routes generated successfully
  [Kamboja] --------------------------------------
  [Kamboja] GET     /
  [Kamboja] --------------------------------------
Ready http://localhost:5000

Open your browser and browse http://localhost:5000

Alt text

Congratulation! you are successfully create your first application using KambojaJS