46
NODEJS WEB DEVELOPMENT By Irfan Maulana | SDE at Blibli.com

PHP Indonesia - Nodejs Web Development

Embed Size (px)

Citation preview

Page 1: PHP Indonesia - Nodejs Web Development

NODEJS WEB DEVELOPMENTBy Irfan Maulana | SDE at Blibli.com

Page 2: PHP Indonesia - Nodejs Web Development

About me• Name : Irfan Maulana• Work : PT. Global Digital Niaga (Blibli.com)• Role : Software Development Engineer• Full-stack developer in Java and Javascript

Page 3: PHP Indonesia - Nodejs Web Development

Outline1. Intro2. Getting Know NodeJS3. Power of NodeJS4. Web Development with NodeJS5. Demo 6. Bundling Assets in NodeJS (*depend with time)7. Unit Test (*depend with time)

Page 4: PHP Indonesia - Nodejs Web Development

1. INTRO

Page 5: PHP Indonesia - Nodejs Web Development

Are you Javascript developer ?

Page 6: PHP Indonesia - Nodejs Web Development

JS Popularity

Source : stackoverflow

Page 7: PHP Indonesia - Nodejs Web Development

Before Node

Page 8: PHP Indonesia - Nodejs Web Development

After Node

Page 9: PHP Indonesia - Nodejs Web Development

2. GETTING KNOW NODEJS

Page 10: PHP Indonesia - Nodejs Web Development

Nodejs Wiki• JavaScript runtime built on Chrome's V8 JavaScript

engine.

Page 11: PHP Indonesia - Nodejs Web Development

Native JS v Nodejs

Nodejs live in different side with native js

FRONT-SIDE BACK-SIDE

Page 12: PHP Indonesia - Nodejs Web Development

Man Behind• Originally written in 2009 by Ryan Dahl (github.com/

ry) and demonstrate in European JSConf November 8 2009• Inspired by file upload progress bar on flickr

Page 13: PHP Indonesia - Nodejs Web Development

3. POWER OF NODEJS

Page 14: PHP Indonesia - Nodejs Web Development

Isomorphic Javascript• Isomorphic from the greek “isos” for equal and

“morph” for shape• When backend and frontend share the same code

Page 15: PHP Indonesia - Nodejs Web Development

Non-Blocking I/O• Not like a traditional thread base I/O, nodejs using

single thread as a native Javascript has do• In thread base, you must allocate maximal thread in

your webserver and every thread will execute one task in one moment, if there another request will be execute after first request has done• Nodejs’s thread can execute other request without

waiting, it will return callbacks that will be only filled when request has done.• Callback that provide is not always the Data, it can

be an exception

Page 16: PHP Indonesia - Nodejs Web Development

Async Request Handler

Source : strongloop

Page 17: PHP Indonesia - Nodejs Web Development

NPM• Include when you install node• You can install and manage all your dependency

easier

Page 18: PHP Indonesia - Nodejs Web Development

Third-Party Library• Have a goodness of JavaScript, easy to extends, easy

to rebuild your own library• In npmjs.com 253167 total packages library• Will be many choices for each library, just choose as

you need

Page 19: PHP Indonesia - Nodejs Web Development

4. WEB DEVELOPMENT WITH NODEJS

Page 20: PHP Indonesia - Nodejs Web Development

Development Stack• Framework : Expressjs• Database : Mongodb (using mongoose module for

connection)• Template Engine : Jade

Page 21: PHP Indonesia - Nodejs Web Development

Expressjs• Express is a minimal and flexible Node.js web

application framework that provides a robust set of features for web and mobile applications. (http://expressjs.com/ )

Page 22: PHP Indonesia - Nodejs Web Development

Expressjs Install• Installing : npm install express• Initial project : express init sampleapp• Install dependency : npm install (in project directory)• Run project : npm start

Page 23: PHP Indonesia - Nodejs Web Development

Expressjs Basic Routing

This route will redirect to index.jade and set data title that will be process in server side by jade

This route will send you json response

Page 24: PHP Indonesia - Nodejs Web Development

Mongoose• elegant mongodb object modeling for node.js (

http://mongoosejs.com/)• Installing : just add dependency in package.json and

run npm install

Page 25: PHP Indonesia - Nodejs Web Development

MongoDB Connection

Just require mongoose depedency and connect with mongoodb in one line code.

Page 26: PHP Indonesia - Nodejs Web Development

Schema and Model

Schema is our mongoDB collection (table) structure wrapper.Model is object that we use for querying.

Page 27: PHP Indonesia - Nodejs Web Development

Using Model for Query

• We using product model for find (*select all) data• This route will return json of all data in table Product• Access in URL / with method GET

Page 28: PHP Indonesia - Nodejs Web Development

Jade - Template Engine• Lightweight templating engine• Adopt JSON structure instead XML• http://jade-lang.com/

Page 29: PHP Indonesia - Nodejs Web Development

Jade v HTML

In jade, you never worry about close tag that spent your time.Just indent like you see HTML

Page 30: PHP Indonesia - Nodejs Web Development

Jade for snippetWe create layout.jade as our main layout, we define block css, content and javascript that will use later

Page 31: PHP Indonesia - Nodejs Web Development

Using snippet• Here we use

our layout.jade and fill our block content, javascript we defined• We can split

our big page, into smaller block than just include that file

Page 32: PHP Indonesia - Nodejs Web Development

Server Renderer

Page 33: PHP Indonesia - Nodejs Web Development

5. DEMOFork on : https://github.com/mazipan/nodejs-simple-restfull-with-express

Page 34: PHP Indonesia - Nodejs Web Development

6. BUNDLING ASSETS IN NODEJS

Page 35: PHP Indonesia - Nodejs Web Development

Grunt• The Javascript Task Runner (http://gruntjs.com/)• Why ? • In one word: automation. The less work you have to

do when performing repetitive tasks like minification, compilation, unit testing, linting, etc, the easier your job becomes.• Install CLI : npm install -g grunt-cli • Added grunt and grunt plugin in devDepedencies

Page 36: PHP Indonesia - Nodejs Web Development

Configure Task• Create Gruntfile.js• Initial configure your task• Load your task• Register your task

Page 37: PHP Indonesia - Nodejs Web Development

Uglify• Javascript assets minifying and bundling into one file if

needed

Page 38: PHP Indonesia - Nodejs Web Development

CSS Minify• CSS assets minifying and concating

Page 39: PHP Indonesia - Nodejs Web Development

7. UNIT TEST

Page 40: PHP Indonesia - Nodejs Web Development

Our Module for Test• Supertest is our agent that will access url method like

postman• Should is easy understand assertion• Mocha is our main test case• Install mocha before : npm install –g mocha• Run : mocha test or node test (in project directory)

Page 41: PHP Indonesia - Nodejs Web Development

Setup depedency

• Setup devDependency in package.json• You can setup script test execution for using node test

Page 42: PHP Indonesia - Nodejs Web Development

Sample Unit Test

Page 43: PHP Indonesia - Nodejs Web Development

GITHUB REPO & CONTACT ME

Page 45: PHP Indonesia - Nodejs Web Development

Contact me

mazipanneh.wordpress.com

@Maz_Ipan

/mazipanneh

/in/irfanmaulanamazipan

[email protected]

Page 46: PHP Indonesia - Nodejs Web Development