- Adding new user:
adduser {username}
- Adding user to sudo group (seperti administrator)
gpasswd -a {username} sudo
- Change user from root
su - {username}
- Update package dengan versi terbaru seandainya ada, atau updating packages dari repository
sudo apt-get update
- Upgrade software dengan versi terbaru
sudo apt-get upgrade
- Memberi nama kepada host (hostname)
echo "{yourHostname}" > /etc/hostname
- Editing hosts /etc/hosts file
nano /etc/hosts
- Editing local timezone
dpkg-reconfigure tzdata
- Setelah kamu menginstall MongoDB, kamu bisa men-run mongodb server dengan
sudo service mongodb start
- Untuk melihat daftar program yang sedang berjalan 'running processes'
ps -ef
- Untuk menghentkan suatu program atau running process
kill {processID}
ataukill -9 {processID}
- Seandainya kamu menggunakan 'putty' untuk mengakses remote server, pasti kamu tahu bahwa ketika kita mengakhiri suatu session, maka program yang berjalan di foreground akan mati. Untuk mengakalinya, kita bisa menggunakan programm 'screen'. Setelah kamu menginstall screen
sudo apt-get install screen
, kamu bisa memulai screen dengan mengetikscreen
dan untuk keluar dari session tanpa mematikan process yang sedang berjalan kamu bisa men-detach screen denganCTRL + A + D
. untuk kembali masuk ke screen yang tadi kamu detach, kamu bisa ketikscreen -r {screenID}
untuk men-rettach screen tersebut
23 September 2017
Linux Ubuntu Server Important Commands
Using Retrofit to build HTTP Client on Android
Untuk berkomunikasi dengan remote server, kamu bisa menggunakan java 3th party library 'Retrofit'. Dengan library ini kamu bisa mengirim dan menerima data ke dan dari remote server dalam bentuk JSON. Retrofit adalah library favorit saya karena mudah sekali untuk menggunakannya. Disini saya akan merangkum step-step bagaimana kamu bisa menggunakan retrofit dengan cepat di project kamu
Terkadang kamu harus membuat suatu class tambahan yang bisa menampung respon object dari server. Misal, kamu mengirim ("POST") user data melalui User object, akan tetapi jawaban (respond) dari server memiliki properties yang berbeda dari User class. Kamu bisa membuat
- Tambahkan internet permission ke dalam manifest XML file.
- Tambahkan Retrofit dan Gson kedalam gradle build file
- Buat Object class yang akan di kirim atau di terima sebagai JSON file, jangan lupa bahwa semua fieldnya harus di declared dengan tipe String
- Buat ApiClient class
- Definisikan API Interface
- Gunakan ApiClient di Aplikasi kamu
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.appName"> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
... | |
</activity | |
</application> | |
</manifest> |
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
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:26.+' | |
compile 'com.squareup.retrofit2:retrofit:2.3.0' | |
compile 'com.squareup.retrofit2:converter-gson:2.3.0' | |
testCompile 'junit:junit:4.12' | |
} |
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
public class User { | |
//dont forget to add @SerializedName annotation, the parameter is the Objectproperty name | |
@SerializedName("name") | |
private String name; | |
@SerializedName("email") | |
private String email; | |
//getters | |
public String getName() { | |
return name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
} |
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
public class ApiClient { | |
//this is the localhost url if you run the app using emulators | |
public static final String BASE_URL = "http://10.0.2.2/"; | |
public static Retrofit retrofit = null; | |
public static Retrofit getApiClient(){ | |
if(retrofit == null){ | |
retrofit = new Retrofit.Builder().baseUrl(BASE_URL). | |
addConverterFactory(GsonConverterFactory.create()).build(); | |
} | |
return retrofit; | |
} | |
} |
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
public interface ApiInterface { | |
// the parameter of get is the endpoint of the url. | |
// if your url is http://localhost:3000/users will looks like this | |
// Use a 'List' if you are fetching an array of Users | |
@GET("users") | |
Call<List<User>> getUsers(); | |
} |
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
private fetchData(){ | |
apiInterface = ApiClient.getApiClient().create(ApiInterface.class); | |
Call<List<User>> call = apiInterface.getSomething(); | |
call.enqueue(new Callback<List<User>>() { | |
@Override | |
public void onResponse(Call<List<User>> call, Response<List<User>> response) { | |
List<User> list = response.body(); | |
Log.d(TAG, "onResponse: " + list.get(0).getText()); | |
} | |
@Override | |
public void onFailure(Call<List<User>> call, Throwable t) { | |
Log.d(TAG, "onFailure: " + t.getMessage()); | |
} | |
}); | |
} |
Terkadang kamu harus membuat suatu class tambahan yang bisa menampung respon object dari server. Misal, kamu mengirim ("POST") user data melalui User object, akan tetapi jawaban (respond) dari server memiliki properties yang berbeda dari User class. Kamu bisa membuat
Class Result
yang bisa menampung respon dari server kemudian kamu bisa menambahkan Result Object kedalam Class User
. Tool yang saya pakai untuk membuat Result class ini adalah JsonSchemaToPOJO Caranya mudah, masukan respon (JSON) yang kamu dapatkan dari aplikasi POSTMAN kedalam formular yang ada di Json2Pojo. 22 September 2017
NPM modules for a simple web application
- Express Nodejs backend framework untuk applikasi web. Memudahkan kamu untuk menghandle routes, get and post requests. untuk menginstall ketik
- Handlebar Adalah javascript templating engine dengan syntax yang sangat simple. Dengan menggunakan syntax
- Nodemon
- Request
- Yargs
- Mongoose
- Body-parser
- Lodash
- validator
- jsonwebtoken
- bcrypt
npm install express --save
{{yourObjectProperty}}
. Untuk mengkombinasikan express.js dan handlebars.js kamu harus menginstall hbs packaged module dari npm. npm install hbs --save
20 September 2017
MongoDB important commands
- Setelah kamu sukses menginstall MongoDB, kamu bisa masuk ke 'bin' directory dimana MongoDB diinstall. Di Windows biasanya directory ini berada di
C:\Program Files\MongoDB\Server\YourMongoDBVersion\bin
didalamnya kamu bisa menyalakan db server dengan mengetikmongod.exe --dbpath \Masukan\Path\Folder\pilihan\kamu
lalu tekan enter. - Kalian bisa berkomunikasi dengan database dengan men-run
mongo.exe
di bin directory, atau dengan menggunakan mongo UI 'Robo 3T' (formerly 'Robomongo')
Node important commands
Starting a Project
Untuk memulai node project kita harus membuat directory, dan melalui command line, kita masukannpm init
untuk memulai sebuah node project. Setelah menginisialisasi npm kita nantinya bisa menginstal npm modules ke applikasi yang kita buat. Debugging:
- Untuk debugging, gunakan module nodemon, sehingga setiap perubahan pada code langsung bisa kita lihat effekt running time nya. Nodemon merestart programm kita setiap kali kita melakukan perubahan. cara menginstall
npm install -g nodemon
-g disini berarti globalnodemon [your node app]
untuk menjalankannya.- Debugging tool dengan menggunakan chrome browser
nodemon --inspect-brk [your node app]
lalu buka chrome browser dan ketikchrome://inspect
disini kamu akan melihat app kamu di list di 'Remote Target' section. Setelah debugging window terbuka, kamu bisa mengklick di line number untuk meletakan stop points
19 September 2017
ES6 Features
ES6-string-injection
Di ES6 Ada cara mudah memasukan variable ke dalam String. Kalian cukup menggunakan tanda`Put your String here and add your variable like this ${yourVariable}`
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
var note = notes.addNote(argv.title, argv.body); | |
if(note){ | |
console.log("successfully saving a new note"); | |
console.log("____"); | |
//ES6 javascript string injection | |
console.log(`Title: ${note.title}`); | |
console.log(`Body: ${note.body}`) | |
}else{ | |
console.log("A note with this title is already exist"); | |
} |
Variable 'let'
Perbedaan utama di variable let adalah pada scope-nya.let
syntax memungkinkan kita untuk mengimplementasi local variable dengan mudah.
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
function doSth() { | |
//tuce is *not* visible out here | |
for( let i = 0; i < 5; i++ ) { | |
//i hanya terlihat di scope ini saja | |
} | |
//i tidak terlihat disini | |
} | |
function byE40() { | |
//variable i2 terlihat disini | |
for( var i2 = 0; i2 < 5; i2++ ) { | |
//var i2 is visible to the whole function | |
} | |
//dan juga disini | |
} |
Arrow Function
Arrow function sangat memudahkan kita menulis code di javascript. Tapi penggunaan arrow function saya sarankan hanya seandainya kamu sudah tau menulis fungsi tanpa menggunakan arrow function. karena penggunaan arrow function akan membingungkan tanpa memahami proses apa yang terjadi di balik itu.
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
var square = (x) => { | |
var result = x*x; | |
return result; | |
}; | |
//All can be simplified into one line | |
//without curly braces means you need the expression to be returned. | |
var autoReturn = (x) => x * x ; | |
var withoutReturnValue = (x) => { | |
console.log(x*x); | |
}; | |
//With only one argument you can get rid of the prenthesis | |
var oneArgument = x => x * x; |
Object destructuring
Kalian bisa membuat variable baru dari objects' properties
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
var user = { | |
name: 'Peter', | |
age: 26, | |
}; | |
// create a new variable name from user object. | |
var {name} = user; | |
// with this, you can do such thing like | |
const {MongoClient, ObjectID} = require('mongodb'); | |
// instead of | |
const MongoClient = require('mongodb').MongoClient; | |
const ObjectID = require('mongodb').ObjectID; |
Promises
Untuk menghandle Asynchronous call, kita bisa menggunakan Promise methode. Cara ini lebih baik karena bisa meminimalisir kesalahan ketika berkerja dengan asynchronous methods.
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
// initiating a new instance of Promise | |
// using setTimeout to simulate delay | |
var asyncAdd = (a, b) => { | |
return new Promise((resolve, reject) =>{ | |
setTimeout(() => { | |
if(typeof a === 'number' && typeof b === 'number'){ | |
resolve(a + b); | |
}else{ | |
reject('Arguments must be numbers'); | |
} | |
}, 1500) | |
}); | |
}; | |
//The then method will be called dependent to the result of the somePromise. 1st argument | |
//will only be called if resolve is called and the 2nd method is called if the reject method had been called. | |
asyncAdd(5, 7).then((result) => { | |
console.log('Result: ', result); | |
}, (errorMessage) => { | |
console.log('Error: ', errorMessage); | |
}); | |
// We can also chaining two asynchronous methods like this | |
// We only provide one error handling method 'catch' | |
asyncAdd(5, "7").then((result) => { | |
console.log('Result: ', result); | |
return asyncAdd(result, 33); | |
}).then((result) => { | |
console.log('Should be 45', result) | |
}).catch((errorMessage) => { | |
console.log(errorMessage); | |
}); |
Subscribe to:
Posts (Atom)