NodeJS
Hello World
Catatan: Simpan di file dengan nama helloworld.html
<html>
<head>
<title>Hello World Javascript</title>
<script type="text/javascript">
// mencetak Hello World di developer tools
console.log("Hello World");
Kondisional di Javascript
<html>
<head>
<title>Kondisional di Javascript</title>
<script type="text/javascript">
var umur = 23;
if (umur <= 15){
console.log("masih bocah..");
}
else if (umur > 15 && umur <= 20){
console.log("beranjak remaja..");
}
else if (umur > 20 && umur <= 30){
console.log("sudah dewasa..");
}
Pengulangan di Javascript
Catatan: Simpan di file dengan nama pengulangan.html
<html>
<head>
<title>Pengulangan di Javascript</title>
<script type="text/javascript">
var banyak_baris = 100;
for (i=0; i < 100; i++){
console.log("ini adalah baris ke - " + i);
document.write("ini adalah baris ke - " + i + "<br/>");
}
</script>
</head>
<body>
</body>
</html>
Fungsi di Javascript
Catatan: Simpan di file dengan nama fungsi.html
<html>
<head>
<title>Fungsi di Javascript</title>
<script type="text/javascript">
function hello(name){
console.log("Hello " + name + " :D....<br/>");
document.write("Hello " + name + " :D....<br/>");
}
var students = ['ghifary', 'afghan', 'danang', 'renisa', 'nuy', 'ani', 'anshar'];
for (student in students){
hello(students[student]);
}
</script>
</head>
<body>
</body>
</html>
Callback di Javascript
Catatan: Simpan di file dengan nama callback.html
<html>
<head>
<title>Fungsi di Javascript</title>
<script type="text/javascript">
// membuat fungsi coba1 untuk digunakan sebagai callback di fungsi hello
function coba1(name){
console.log(name + " ada di dalam callback 1...<br/>");
document.write(name + " ada di dalam callback 1...<br/>");
}
// membuat fungsi coba2 untuk digunakan sebagai callback di fungsi hello
function coba2(name){
console.log(name + " ada di dalam callback 2...<br/>");
document.write(name + " ada di dalam callback 2...<br/>");
}
----------------------------------------------------------------
Callback di Javascript
Catatan: Simpan di file dengan nama callback.html
---------------------------------------------------------------// fungsi hello adalah fungsi yang akan menggunakan fungsi lain sebagai callback
function hello(name, callback){
console.log("Hello " + name + " :D....<br/>");
document.write("<b>Hello " + name + " :D....</b><br/>");
callback(name);
}
var students = ['ghifary', 'afghan', 'danang', 'renisa', 'nuy', 'ani', 'anshar'];
for (student in students){
// memanggil callback cukup dengan melewatkan fungsi sebagai parameter
hello(students[student], coba1);
hello(students[student], coba2);
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title>Menangani Event di Javascript</title>
<script type="text/javascript">
// membuat fungsi yang digunakan saat salah satu field disorot
function sorot_handler(elem, target, perintah){
petunjuk = document.getElementById(target);
petunjuk.innerHTML = perintah;
console.log('field ' + elem.name + ' lagi disorot....');
}
// membuat fungsi yang digunakan saat salah satu field tidak disorot
function tidak_disorot_handler(elem, target){
petunjuk = document.getElementById(target);
petunjuk.innerHTML = "";
console.log('field ' + elem.name + ' tidak disorot...');
};
-------------------------------------------------------------
------------------------------------------------------------<body>
<form id="form-students" onsubmit="submit_handler();">
Nama: <input type="text" name="nama" id="nama"
onmouseover="sorot_handler(this, 'petunjuk-nama', 'isi field berikut dengan nama siswa');"
onmouseout="tidak_disorot_handler(this, 'petunjuk-nama');"/> <br/>
<div id="petunjuk-nama"></div>
Program Studi: <input type="text" name="prodi" id="prodi"
onmouseover="sorot_handler(this, 'petunjuk-prodi', 'isi field berikut dengan program studi siswa');"
onmouseout="tidak_disorot_handler(this, 'petunjuk-prodi');"/> <br/>
<div id="petunjuk-prodi"></div>
<input type="submit" value="Simpan" /><input type="reset" value="Ulangi" onclick="klik_handler();"/>
</form>
<div id="student-info">
</div>
</body>
</html>
<html>
<head>
<title>Manipulasi DOM Sederhana</title>
<script type="text/javascript">
// membuat fungsi yang mengubah warna teks pada paragraf
function mengubah_warna(){
paragraf = document.body.getElementsByTagName("p");
for (i=0;i<paragraf.length; i++){
paragraf[i].style.setProperty('background-color', 'red');
paragraf[i].style.setProperty('color', 'white');
}
}
</script>
</head>
-------------------------------------------------------------
------------------------------------------------------------<body>
<form>
<input type="button" onclick="mengubah_warna();" value="Ubah Warna !"/>
<p>1. Ini akan berwarna merah !</p>
<p>2. Ini akan berwarna merah !</p>
<p>3. Ini akan berwarna merah !</p>
<p>4. Ini akan berwarna merah !</p>
<p>5. Ini akan berwarna merah !</p>
</form>
</body>
</html>
-------------------------------------------------------------
------------------------------------------------------------if (nim.value.match(nim_ck)){
nim.style.setProperty('border-color', '');
error_nim.innerHTML = "";
student_info.innerHTML = "Validasi berhasil...";
}
else{
nim.style.setProperty('border-color', 'red');
error_nim.innerHTML = "<p>nim harus diisi. tidak boleh selain angka</p>";
student_info.innerHTML = "Validasi gagal...";
}
}
</script>
-------------------------------------------------------------
------------------------------------------------------------</head>
<body>
<form id="form-students">
NIM: <input type="text" name="nim" id="nim" /> <br/>
<div id="error-nim"></div>
<input type="button" value="Simpan" onclick="validasi();"/><input type="reset" value="Ulangi"/>
</form>
<div id="student-info">
</div>
</body>
</html>
Instalasi ?
Hello World
Catatan: Simpan di file dengan nama hello.js
console.log('Hello World');
console.log('Anda sedang menggunakan NodeJS...');
console.log('NodeJS adalah javascript yang berada disisi server');
Hello World
Catatan: Simpan di file dengan nama hello.js
node namafile.js
Request Handling
Catatan: Simpan di file dengan nama request-handling.js
var http = require('http');
var server = http.createServer(function(req, res){
console.log(req.url);
if (req.url == '/'){
res.write('Welcome to public page');
res.end();
}
else if (req.url == '/login'){
res.write('This is a login page');
res.end();
}
else if (req.url == '/category'){
res.write('This is a jobs category page');
res.end();
}
--------------------------------------------------
Request Handling
Catatan: Simpan di file dengan nama request-handling.js
-------------------------------------------------else if (req.url == '/jobs'){
res.write('This is a jobs page');
res.end();
}
else if (req.url == '/about'){
res.write('This is about page');
res.end();
}
else{
res.write('Page not found');
res.end();
}
});
server.listen(8084);
console.log('Server is running on port 8084');
Request Handling
Catatan: Simpan di file dengan nama request-handling.js
Hello MySQL
Hello MySQL
Hello MySQL
Catatan: Simpan di file dengan nama hellomysql.js
Hello MySQL
Catatan: Simpan di file dengan nama hellomysql.js
Hello HTML
Hello HTML
Catatan: Simpan di file dengan nama html-swig.html
<html>
<head>
<title>Hello HTML dengan Swig</title>
</head>
<body>
<h1>Daftar Mahasiswa</h1>
{% for mhs in mahasiswa %}
{{ mhs.nim }}, {{ mhs.nama }}, {{ mhs.email }}, {{ mhs.prodi }} <br/>
{% endfor %}
</body>
</html>
Hello HTML
Catatan: Simpan di file dengan nama html-swig.js
Hello HTML
Catatan: Simpan di file dengan nama html-swig.js
-----------------------------------------------------------------------------------if (req.url == '/'){
connection.connect();
connection.query('select * from mahasiswa', function(err, rows, fields){
if (err) throw err;
template = swig.compileFile('/home/ridwanbejo/Projects/NodeJS\ Mania/labs/node-basic/html-swig.html');
output = template({mahasiswa: rows});
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(output);
res.end();
});
connection.end();
}
});
server.listen(8084);
console.log('Server is running on port 8084');
Hello HTML
Pastikan file html-swig.html yang telah ditulis, berada satu tempat dengan
file html-swig.js
Karena kita akan melihat halaman web yang diberikan oleh web server
NodeJS, maka jalankan dulu file html-swig.js dengan cara: node htmlswig.js
Bukalah browser kemudian akses http://localhost:8084
<html>
<head>
<title>Mengirim FORM dengan Method POST</title>
</head>
<body>
<form action="/mahasiswa/add/process" method="POST">
Nama: <input type="text" name="nama" style="margin-bottom:20px;" /> <br/>
NIM: <input type="text" name="nim" style="margin-bottom:20px;" /> <br/>
E-Mail: <input type="text" name="email" style="margin-bottom:20px;" /> <br/>
Program Studi: <input type="text" name="prodi" style="marginbottom:20px;" /> <br/>
<input type="submit" value="Tambah"/>
</form>
</body>
</html>
Kesimpulan
Referensi
Google it:
NodeJS Succintly
NodeJS Up and Running
(http://chimera.labs.oreilly.com/books/1234000001808/i
ndex.html)
Node Book (http://book.mixu.net/node/)