SlideShare a Scribd company logo
Ngaji NodeJs
GLiB
http://glibogor.or.id
Minggu, 8 November 2015
Javascript
● ECMA Script
● Bahasa skrip, ada intreperter
● Dynamic Type
NodeJS
● Bukan bahasa, bukan framework.
● Jalan di atas mesin V8 dari Google
● Terdiri dari :
– Runtime environment
– API / pustaka bawaan (beberapa berbeda
dengan API / pustaka bawaan di peramban)
●
Mengapa?
● Sementara ini paling populer di statistik GitHub
● Perkakas pengembangan front-end banyak
yang bergantung ke modul NPM
● Bahasa yang sama depan belakang
● Non-blocking IO
● Asyik buat aplikasi berbasis jaringan (http,
websocket)
Mengapa jangan?
● Basis data relasional
● Komputasi berat
Pasang
● Paket manajer dari sistem operasi (apt, yum,
brew)
● NVM (Node Version Manager)
● Kompilasi sendiri
Fungsi
var cetak = function(string){
console.log(string);
}
cetak(“hai”);
Fungsi
var foo = false;
var assignTrue = function(){
return true;
}
foo = assignTrue();
console.log(foo);
Fungsi
function cetak(string1, string2) {
console.log(string1 + “ “ + string2);
}
cetak(“hai”, “kamu”);
cetak(1, “orang”);
Callback
var cetak = function(string, cb){
console.log(string);
if (cb) {
cb();
}
}
cetak(“assalamualaikumn”, function(){
console.log(“waalaikumsalamn”);
});
cetak(“hain”);
Callback
cetak(“assalamualaikumn”, function(){
cetak(“waalaikumsalamn”, function(){
console.log(“kapan?n”);
});
});
Callback
var splitName = function(fullName, callback){
var firstName = fullName.split(“ “)[0];
var lastName = fullname.split(“ “)[1];
callback(firstName, lastName);
}
splitName(“peregrin took”, function(first, last){
console.log(first + “ “ + last);
})
Common Callback Pattern
var isOdd = function(int, cb){
var err;
var result;
if (typeof int != “number”) {
err = new Error(“Not a number!”);
return cb(err);
}
if (int % 2 == 0) {
cb(null, true);
} else (int % 2 == 1) {
cb(null, false);
}
}
isOdd(function(err, result){
if (err) {
return console.log(result);
}
console.log(result);
})
Callback Hell
cetak1(input, function(result){
cetak2(result, function(result){
cetak3(result, function(result){
cetak4(result, function(result){
cetak5(result, function(result){
console.log(result);
});
});
});
});
});
var foo = function(input){
return new Promise(function(resolve, reject){
var result;
try {
// do something that has chance to throws an error
} catch(err) {
return reject(err);
}
resolve(result);
})
}
foo(input)
.then(function(result){
// Handle result
})
.catch(function(err){
// Handle error
});
Promise
cetak(input)
.then(function(result){
return fungsi1(result);
})
.then(function(result){
return fungsi2(result);
})
.then(function(result){
return fungsi3(result);
})
.then(function(result){
return fungsi4(result);
})
.then(function(result){
return fungsi5(result);
})
.then(function(result){
console.log(result);
})
.catch(function(err){
// Handle error
});
Sync vs Async
Sync
console.log(1);
console.log(2);
console.log(3);
console.log(4);
var a = foo1();
var b = foo2(a);
var c = foo3(b);
var d = foo4(c);
Async
console.log(1);
getNumber(function(number){
console.log(number); // number = 2
// do some thing ...
});
console.log(3);
console.log(4);
Async
var data = “hai kamu ~”;
sendSMS(data, function(err, result){
// save sent sms to sent box,
// notify sender
// do some thing...
});
saveToDB(data, function(err, result){
// emit a socket communication
// do some thing other
});
Command Argument
console.log(process.argv[1]);
-------------------------------------
$ node cetak.js “halo”
Environment Variable
console.log(“Running on :”);
console.log(process.env.HOST + “:” process.env.PORT);
-------------------------------------
$ HOST=10.0.0.1 PORT=3001 node index.js
fs API
Sync
----------------------------------------------
var fs = require(“fs”);
var ls = fs.readdirSync(“./”);
Async
----------------------------------------------
var fs = require(“fs”);
var ls;
fs.readdir(function(err, result){
if (err) {
// Handle error
}
ls = result;
})
Require
cetak.js
–------------------------------------------
var cetak = function(){
console.log(“yooo!”);
}
module.exports = cetak;
index.js
-------------------------------------------
var cetak = require(“cetak”);
cetak();
Require
cetak.js
–------------------------------------------
var hai = function(){
console.log(“hai”);
}
var halo = function(){
console.log(“halo”);
}
exports.hai = hai;
exports.halo = halo;
index.js
-------------------------------------------
var cetak = require(“cetak”);
cetak.hai();
cetak.js
–------------------------------------------
var Cetak = function(){}
Cetak.prototype.halo = function(){
console.log(“halo”);
}
Cetak.prototype.hai = function(){
console.log(“hai”);
}
module.exports = Cetak;
index.js
-------------------------------------------
var Cetak = require(“cetak”);
var cetak = new Cetak();
cetak.halo();
NPM
● npmjs.org
$ npm install wooosaaah
NPM
index.js
-------------------------------------------
var md5 = require(“md5”);
md5(“hai”);
NPM
● npm init
● npm install --save namapaket
● npm install –save-dev namapaket
Bangun
● Webserver sederhana
Webserver
index.js
-------------------------------------------
var express = require("express"),
app = express();
app.get("/", function(req, res) {
res.send("website webdong");
});
app.listen(3000, "localhost", function(){
console.log("Webnya udah jalandong");
});

More Related Content

PDF
ECMA2015 INSIDE
KEY
NrStage 사용하기
PDF
Collection pipeline par Mathieu Godart
PDF
Node.js moduly a testovanie
PDF
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
DOCX
Memanggil prosedur sendiri dari program utama 1
PPTX
Herpiko Dwi Aguno - PKIWebSDK : Pustaka JavaScript untuk aplikasi PKI berbasi...
ODP
Kelas Bootstrap Basic
ECMA2015 INSIDE
NrStage 사용하기
Collection pipeline par Mathieu Godart
Node.js moduly a testovanie
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Memanggil prosedur sendiri dari program utama 1
Herpiko Dwi Aguno - PKIWebSDK : Pustaka JavaScript untuk aplikasi PKI berbasi...
Kelas Bootstrap Basic

More from Herpiko Dwi Aguno (6)

PDF
Parallel Computing Example with Raspberry Pi Cluster
ODP
RumahPintar Prototype
ODP
Urai paper : Indonesian Stemming
ODP
Blankon Installer
ODP
Sistem informasi Pemeliharaan Aset
ODP
Contoh Kasus Manajemen Konflik
Parallel Computing Example with Raspberry Pi Cluster
RumahPintar Prototype
Urai paper : Indonesian Stemming
Blankon Installer
Sistem informasi Pemeliharaan Aset
Contoh Kasus Manajemen Konflik
Ad

Recently uploaded (7)

PDF
فورمولر عمومی مضمون فزیک برای همه انجنیران
PDF
Materi seni rupa untuk sekolah dasar materi tentang seni rupa
PPTX
Tahfidz Qur’an TIMING tampa musik bagian 2.pptx
PPTX
science grade 7 quiz_Scientific Method.pptx
PDF
ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯSri Kshetra Champakadham Swamy Temple
PDF
15 AUG 2025 PS 15 AUG 2025 PS 15 AUG 2025 PS
PPTX
Coklat Beige Ilustrasi 3 Dimensi Tugas Kelompok Presentasi.pptx
فورمولر عمومی مضمون فزیک برای همه انجنیران
Materi seni rupa untuk sekolah dasar materi tentang seni rupa
Tahfidz Qur’an TIMING tampa musik bagian 2.pptx
science grade 7 quiz_Scientific Method.pptx
ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯSri Kshetra Champakadham Swamy Temple
15 AUG 2025 PS 15 AUG 2025 PS 15 AUG 2025 PS
Coklat Beige Ilustrasi 3 Dimensi Tugas Kelompok Presentasi.pptx
Ad

Ngaji NodeJs