Anh Thang Bui

11 Oct 2017 ~ 1 min read

Hello World! app with Node.js and Koa


Install Node.js for your platform

Open terminal and type

mkdir hello-world; cd hello-world

Initialize your project and link to npm

Running this command initializes your project:

npm init

This creates a package.json file in your hello-world app folder and will prompt you to enter number of things. You can enter your way through all of them EXCEPT this one:

entry point: (index.js)

You will want to change this to

<your_file_name>.js

Install Koa in the hello-world directory

npm i koa koa-router

--save is default option in node v8

Start your text editor and create a file named as entry point

const Koa = require('koa')
const Router = require('koa-router')

const app = new Koa()
const router = new Router()

router.get('/', (ctx) => {
    ctx.status = 200
    ctx.body = 'Hello, World'
})

app.use(router.routes())

app.listen(3000, () => {
    console.log('app listening on port 3000')
})

Run the app

node <your_file_name>.js

Cheers!


Anh Thang Bui

Hi, I'm Thang.
I'm a software engineer from Hanoi, Vietnam. You can follow me on Twitter, see some of my work on GitHub, or read more about me on my website.