Last active
December 15, 2016 04:00
-
-
Save birowo/135a6cdc009b2fcbae78e677063a9bc9 to your computer and use it in GitHub Desktop.
konsep middleware di express.js . pakai npm install express.js . buat file skrip misal: coba_express.js seperti berikut: . jalankan dari terminal: >node coba_express . dari browser akses: http://localhost:3000 . lihat diterminal console.log: acedb . jika ingin buat seperti express.js harus faham konsep/cara-kerja express.js , versi sederhananya …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/*jika kita buat sendiri yang seperti express.js , versi sederhananya seperti ini: */ | |
function ekpres(){ | |
const http = require('http'); | |
let middlewares = null, len = 0; | |
function onRekuesCB(rekues, respon){ | |
let idx = -1; | |
!function lanjut(){ | |
if(++idx < len)return middlewares[idx](rekues, respon, lanjut); | |
}(); | |
} | |
function pakai(...mws){ | |
middlewares = mws; | |
len = mws.length; | |
return http.createServer(onRekuesCB); | |
} | |
return {pakai: pakai} | |
} | |
function ab(rekues, respon, lanjut){ | |
console.log('a'); | |
lanjut(); | |
console.log('b'); | |
} | |
function cd(rekues, respon, lanjut){ | |
console.log('c'); | |
lanjut(); | |
console.log('d'); | |
} | |
function e(rekues, respon, lanjut){ | |
console.log('e'); | |
respon.end(); | |
} | |
ekpres().pakai(ab, cd, e).listen(3000); | |
/* | |
console.log: | |
a | |
c | |
e | |
d | |
b | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const express = require('express'); | |
function ab(req, res, next){ | |
console.log('a'); | |
next(); | |
console.log('b'); | |
} | |
function cd(req, res, next){ | |
console.log('c'); | |
next(); | |
console.log('d'); | |
} | |
function e(req, res, next){ | |
console.log('e'); | |
res.end(); | |
} | |
express().use(ab, cd, e).listen(3000); | |
/* | |
console.log: | |
a | |
c | |
e | |
d | |
b | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function midWareAlt(){ | |
const http = require('http'); | |
function pakai(...middlewares){ | |
let idx = middlewares.length - 1, mw = middlewares[idx](); | |
while(idx > 0) mw = middlewares[--idx](mw); | |
return http.createServer(mw); | |
} | |
return {pakai: pakai} | |
} | |
/*berikut saya buat alternatif middleware dengan cara yang berbeda(function<induk> return function<anak>) dengan yang dipakai express.js*/ | |
/*fungsi-induk dipakai oleh method pakai untuk pass-by-reference fungsi-anak(middleware) yang berada di fungsi-induk berikutnya*/ | |
/*jadi supaya middleware mengetahui reference middleware berikutnya*/ | |
function ab(lanjut){ | |
return function AB(rekues, respon){ | |
console.log('a'); | |
lanjut(rekues, respon); | |
console.log('b'); | |
} | |
} | |
function cd(lanjut){ | |
return function CD(rekues, respon){ | |
console.log('c'); | |
lanjut(rekues, respon); | |
console.log('d'); | |
} | |
} | |
function e(lanjut){ | |
return function E(rekues, respon){ | |
console.log('e'); | |
respon.end(); | |
} | |
} | |
midWareAlt().pakai(ab, cd, e).listen(3000); | |
/* | |
console.log: | |
a | |
c | |
e | |
d | |
b | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/*pakai bind*/ | |
function midWareAlt2(){ | |
const http = require('http'); | |
function pakai(...middlewares){ | |
let idx = middlewares.length - 1, mw = middlewares[idx]; | |
while(idx > 0) mw = middlewares[--idx].bind(mw); | |
return http.createServer(mw); | |
} | |
return {pakai: pakai} | |
} | |
function ab(rekues, respon){ | |
console.log('a'); | |
this(rekues, respon); | |
console.log('b'); | |
} | |
function cd(rekues, respon){ | |
console.log('c'); | |
this(rekues, respon); | |
console.log('d'); | |
} | |
function e(rekues, respon){ | |
console.log('e'); | |
respon.end(); | |
} | |
midWareAlt().pakai(ab, cd, e).listen(3000); | |
/* | |
console.log: | |
a | |
c | |
e | |
d | |
b | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const http = require('http'); | |
function ekpres(){ | |
function pakai(...mws){ | |
function Lanjut(mw, lanjut){ | |
return function(){ | |
this.lanjut = lanjut; | |
this.mw = mw; | |
this.mw(); | |
} | |
} | |
let idx = mws.length - 1, mw = mws[idx], lanjut = null; | |
while(idx){ | |
lanjut = Lanjut(mw, lanjut); | |
mw = mws[--idx]; | |
} | |
return http.createServer(function (rekues, respon){ | |
const _ = { | |
rekues: rekues, | |
respon: respon, | |
lanjut: lanjut, | |
mw: mw | |
} | |
_.mw(); | |
}); | |
} | |
return {pakai: pakai} | |
} | |
function ab(){ | |
console.log('a'); | |
this.lanjut(); | |
console.log('b'); | |
} | |
function cd(){ | |
console.log('c'); | |
this.lanjut(); | |
console.log('d'); | |
} | |
function e(){ | |
console.log('e'); | |
this.respon.end(); | |
} | |
ekpres().pakai(ab, cd, e).listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
framework yang ada performa masih 15% lebih jelek dibanding tanpa framework. cukup besar juga ya ... mengapa ???
https://raygun.com/blog/2016/12/node-js-performance-comparison/