[ES6] Destructuring

基本用法

var member = {
name: 'Mark';
Phone: 123123;
};

//ES5
var name = member.name;
var Phone = member.Phone;

//ES6
var {name, phone} = member;

過往ES5

var member = {
name: 'Mark';
Phone: 123123;
};

sayMember(member);

// ES5
function sayMember(member){
// 在一些ajax的case中
// response回來的member可能沒有所需要的properties
// 傳統上使用 || operator 設定預設值
var name = member.name || 'John Doe';
var phone = member.phone || 000000;
console.log('Your name is ' + name + ' and your phone is ' + phone)
}

// ES6
function sayMember({name='John Doe', phone = 000000}){
console.log('Your name is ' + name + ' and your phone is ' + phone)
// 變得超簡潔!
}

而且也支援Nested Array or Nested Object!

Ref: MDN – Destructuring_assignment

nginx: unrecognized service

當執行service nginx start,
卻出現錯誤訊息 nginx: unrecognized service

Nginx Init Script
GitHub nginx-init-ubuntu

在/etc/init.d資料夾下, 下载nginx的init script:

$ cd /etc/init.d
$ wget https://raw.githubusercontent.com/JasonGiedymin/nginx-init-ubuntu/master/nginx

增加nginx到系統啟動

$ sudo update-rc.d nginx defaults

如果出現錯誤訊息 insserv: script nginx is not an executable regular file, skipped!
則需更改執行權限

$ sudo chmod +x /etc/init.d/nginx

如果你跟我一樣在多次嘗試中, 已經有設定過了
出現訊息 System start/stop links for /etc/init.d/nginx already exist.
就把之前的設定給移除掉吧!

$ update-rc.d -f nginx remove
Removing any system startup links for /etc/init.d/nginx ...

然後再執行一次sudo update-rc.d nginx defaults

$ echo "NGINX_CONF_FILE=/etc/nginx/nginx.conf" > /etc/default/nginx
$ echo "DAEMON=/usr/bin/nginx" >> /etc/default/nginx

[linux] 解決錯誤訊息 perl: warning: Setting locale failed.

http://imgur.com/a/Wnx0W

yourUserName@ubuntu-14:~$ vim .bashrc
#在.bashrc檔案內增加此行 export LC_ALL="en_US.UTF-8"

然後執行
sudo locale-gen zh_TW.UTF-8
sudo dpkg-reconfigure locales
大功告成!

yourUserName@ubuntu-14:~$ sudo locale-gen zh_TW.UTF-8 
Generating locales...
  zh_TW.UTF-8... done
Generation complete.
yourUserName@ubuntu-14:~$ sudo dpkg-reconfigure locales 
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_PAPER = "lzh_TW",
    LC_ADDRESS = "lzh_TW",
    LC_MONETARY = "lzh_TW",
    LC_NUMERIC = "lzh_TW",
    LC_TELEPHONE = "lzh_TW",
    LC_IDENTIFICATION = "lzh_TW",
    LC_MEASUREMENT = "lzh_TW",
    LC_TIME = "lzh_TW",
    LC_NAME = "lzh_TW",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_ALL to default locale: No such file or directory
Generating locales...
  en_AG.UTF-8... done
  en_AU.UTF-8... done
  en_BW.UTF-8... done
  en_CA.UTF-8... done
  en_DK.UTF-8... done
  en_GB.UTF-8... done
  en_HK.UTF-8... done
  en_IE.UTF-8... done
  en_IN.UTF-8... done
  en_NG.UTF-8... done
  en_NZ.UTF-8... done
  en_PH.UTF-8... done
  en_SG.UTF-8... done
  en_US.ISO-8859-1... done
  en_US.UTF-8... done
  en_ZA.UTF-8... done
  en_ZM.UTF-8... done
  en_ZW.UTF-8... done
  zh_TW.UTF-8... up-to-date
Generation complete.

[linux] adduser 並給予sudo權限

under root

root@ubuntu-14:~$ adduser yourUserName
Adding user `yourUserName' ...
Adding new group `yourUserName' (1000) ...
Adding new user `yourUserName' (1000) with group `hii' ...
Creating home directory `/home/yourUserName' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for hii
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] y

接著

root@ubuntu-14:~$ su yourUserName #切換到yourUserName帳號下
yourUserName@ubuntu-14:~$ sudo apt-get dist-upgrade
[sudo] password for yourUserName: 
yourUserName is not in the sudoers file.  This incident will be reported.
#發現yourUserName無法執行sudo命令

再回到root 下執行visudo

root@ubuntu-14:~$ visudo  #打開設定文件

# User privilege specification
root    ALL=(ALL:ALL) ALL
yourUserName     ALL=(ALL:ALL) ALL #增加此行
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

這樣就完成給予user sudo命令的權限了!
跳去開始使用吧!
root@ubuntu-14:~$ su yourUserName

[vscode, linux] Why url didn’t load in broswer under vistual studio code editor.

解決linux下vscode編輯器內超連結打開瀏覽器時, 頁面無法加載的問題

Why url didn’t load in broswer under vistual studio code editor.

到資料夾 home/yourAccount/.local/share/applications
修改 google-chrome.desktop

增加%U在Exec的後面, 如下

Exec=/opt/google/chrome/chrome %U

然後就可以順利執行了

ref: http://askubuntu.com/questions/499200/xdg-open-opens-chrome-but-does-not-load-the-url

[Shell Script] Exit code

每個指令都會返回一個Exit Status, 通常稱之為Exit code或Return code
Return code的範圍從0-255
其中 0 代表執行成功success
其他不是 0 的數字都是error condition

如何看Return code呢?
$? 代表了前一個執行指令的Return Code

ls /folderNotExisted
ls: cannot access '/folderNotExisted': No such file or directory

echo "$?"
2

寫一個 exiCode.sh

#!/bin/bash
HOST="google.com"
ping -c 1 $HOST && echo -e "\n $HOST reachable."

結果:

PING google.com (216.58.200.238) 56(84) bytes of data.
64 bytes from redirector.googlevideo.com (216.58.200.238): icmp_seq=1 ttl=54 time=17.7 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 17.739/17.739/17.739/0.000 ms

 google.com reachable.

[NodeJS-Stream] 使用highland 將csv轉換成物件

使用 highland 模組將 csv 轉成 object

有一list.csv如下

Mary,09090909,female,25
Mark,2398743092,male,37
John,21312,male,28

 

const fs = require('fs');
const highland = require('highland');

highland(fs.createReadStream('list.csv', 'utf8'))
.split()
.map(line => line.split(','))
.map(part => ({
name: part[0],
number: part[1],
gender: part[2],
age: part[3]
}))
.each(console.log);

輸出如下

{ name: 'Mary', number: '09090909', gender: 'female', age: '25' }
{ name: 'Mark', number: '2398743092', gender: 'male', age: '37' }
{ name: 'John', number: '21312', gender: 'male', age: '28' }

 

參考

[Ngnix] Building nginx from source

參考連結installing nginx open source

Nginx 官網

選擇mainline, 右鍵複製聯結

http://imgur.com/fJsLuVi

使用wget使令下載原始碼

wget http://nginx.org/download/nginx-1.11.3.tar.gz?_ga=1.218425113.1424501337.1485766539

解壓縮

tar -zxvf http://nginx.org/download/nginx-1.11.3.tar.gz?_ga=1.218425113.1424501337.1485766539

安裝library :
– pcre (ajax)
– libssl (SSL & https)
– zlib1g (壓縮靜態檔案)

sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Installation and Compile-Time Options

在你的nginx資料夾下執行configure script, 這段script會產生一個編譯程式碼和安裝nginx所需的Makefile

./configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-debug --with-pcre --with-http_ssl_module

其中 --with-http_ssl_module 是使用的模組
nginx module

yourUserName@ubuntu-14:~/nginx-1.11.3$ ls
CHANGES LICENSE README conf contrib man src
CHANGES.ru Makefile auto configure html objs
#執行完畢後, 可看見增加了 Makefile 檔案和 objs 資料夾

接著執行make (compile and install the build):

make
sudo make install

如果沒有make, 先下指令安裝 <code>apt-get install make</code>
關於make可以看鳥哥的介紹
完成後啟動nginx

sudo nginx

就能看見nginx預設畫面了

http://imgur.com/iB9ZD4A