Skip to content

Instantly share code, notes, and snippets.

@WLun001
WLun001 / express.js
Last active August 5, 2021 12:17
express error handling
const express = require('express');
const app = express();
// cookie parser
// body parser
// cors
// if applicable
app.use('/api/user', userRoute(express.Router()))
@WLun001
WLun001 / Adapter.kt
Created May 20, 2018 06:05
Set OnItemClickListener on RecycleView Android Kotlin
class Adapter(private var hospitalList: ArrayList<MapsHospital>,
private var listener: OnItemClickListener)
: RecyclerView.Adapter<MapsHospitalAdapter.ViewHolder>() {
interface OnItemClickListener {
fun onItemClick(hospital: MapsHospital)
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder?.bindView(hospitalList[position], listener)
@WLun001
WLun001 / request.kt
Created May 20, 2018 05:55
HTTP request with authorisation token Kotlin
private fun httpRequestBuilder(url: String, request: Int): HttpsURLConnection {
val connection = URL(url).openConnection() as HttpsURLConnection
connection.readTimeout = 10000
connection.connectTimeout = 15000
connection.setRequestProperty("Authorization", "Bearer 234567834567")
when (request) {
GET -> {
connection.requestMethod = "GET"
connection.connect()
}
@WLun001
WLun001 / request.kt
Created May 20, 2018 05:51
GET request with custom query text on Android Kotlin Anko
// example with Google Maps
// https://developers.google.com/maps/documentation/distance-matrix/intro
// format : origins=place_id:ChIJ3S-JXmauEmsRUcIaWtf4MzE
companion object {
const val googleApiKey = "123"
const val distanceURL = "https://maps.googleapis.com/maps/api/distancematrix/json?"
}
doAsync {
val placeID = "123"
val location = Uri.encode("3.041803,101.793075")
@WLun001
WLun001 / promise.js
Last active May 20, 2018 06:06
Javascript HTTP POST request with Promise
//creating promise
// more on https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261
function exampleFunc(value) {
return new Promise((resolve, reject) => {
http.get(option, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
}); // store each response chunk
res.on('end', () => {
@WLun001
WLun001 / switchcase.js
Created May 20, 2018 05:24
Javascript advanced Switch Case
let action = 'a'
const actionHandler = {
'a': () => {},
'b': () => {}
};
actionHandler[action]();
@WLun001
WLun001 / sound.swift
Last active May 20, 2018 05:15
iOS 11 Swift 4 Play sound
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate{
var audioPlayer: AVAudioPlayer!
@IBAction func notePressed(_ sender: UIButton) {
playSound("note\(sender.tag)")
}