2020年4月12日 星期日

create a swap file in Btrfs filesystem

I have spent a lot of time to find out how to create a swap file in my Ubuntu 18.04

However, it is still fail until I found the following article.

Swapon failed: Invalid argument on a Linux system with Btrfs filesystem

There is a limitation in Btrfs filesystem.

Why did I choose Btrfs system, that is because I can use snapshot in my NAS only when I choose Btrfs for my QNAP's filesystem.

Anyway, I can use a swapfile in my Ubuntu with Btrfs filesystem.

please reference the following article.
Swap



Reference:
Swapon failed: Invalid argument on a Linux system with Btrfs filesystem
Swap

2020年3月8日 星期日

open the firewall in Ubuntu

開啟防火牆

sudo ufw enable


允許MySQL連線
sudo ufw allow mysql


允許SSH連線
sudo ufw allow ssh


sudo ufw allow 22


參考資料:
UFW Essentials: Common Firewall Rules and Commands
Install MySQL Server on the Ubuntu operating system 

install MySQL in Ubuntu

可以直接參考下面這一個文章

Install MySQL Server on the Ubuntu operating system

安裝light Ubuntu environment package

最近要安裝MySQL,
選擇了在QNAP上的virtual station安裝Ubuntu,
並且在上面安裝MySQL,
為了節省資源,
打算安裝輕量的work environment,
首選就是 Xfce

可以參考下面的資料:
The 8 Best Ubuntu Desktop Environments (18.04 Bionic Beaver Linux)
Install Xfce desktop on Ubuntu 18.04 Bionic Beaver Linux

2020年3月4日 星期三

req與res在node.js的意思

在node.js裡面跟網頁互動的話,
通常會有call back function,
而call back function通常會有兩個參數,
一個是req
一個是res

根據Express API這一個網站說明

Request
The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the object is always referred to as req (and the HTTP response is res) but its actual name is determined by the parameters to the callback function in which you’re working.

Response
The res object represents the HTTP response that an Express app sends when it gets an HTTP request.

參考資料:
Express API

2020年3月2日 星期一

let and var in Node.js

let allows you to declare variables that are limited to a scope of a block statement, or expression on which it is used, unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below).


參考資料:
MDN web docs - let

inherits(繼承)

在node.js裡面也有繼承的方式,
但是,目前看到宣告一個函式可以繼承一個類別
補充:最後發現是宣告一個函式,它會繼承一個類別的建構子(Constructor)
那就表示這一個函式也是一個建講子(Constructor),
它身為一個建構子,就表示它是某一個類別的建構子,
所以,表示你也因為這一個函式inherts宣告了一個新的類別LoopProcessor
因此可以用這一個新的類別建立物件。

這一個方式還蠻特別的,可能要研究一下。
這裡看起來宣告的LoopProcessor繼承了EventEmitter(EventEmitterEventEmitter類別的建構子(Constructor))
例子在下面這裡
Day11 - Node.js EventEmitter


var emitter = require('events').EventEmitter;
// need util module when using inherits
var util = require('util');
 
function LoopProcessor(num) {
    // create a new object for current class
    var me = this;
 
    setTimeout(function () {
 
        for (var i = 1; i <= num; i++) {
            me.emit('BeforeProcess', i);
 
            console.log('Processing number:' + i);
 
            me.emit('AfterProcess', i);
        }
    }
    , 2000)
 
    return this;
}

//LoopProcessor is inherited from emitter class 
util.inherits(LoopProcessor, emitter)
 
var lp = new LoopProcessor(3);
 
lp.on('BeforeProcess', function (data) {
    console.log('About to start the process for ' + data);
});
 
lp.on('AfterProcess', function (data) {
    console.log('Completed processing ' + data);
});


可以參考util的function API
util.inherits

util.inherits(constructor, superConstructor)#
constructor <Function>
superConstructor <Function>

但是,目前並不建議使用util.inherits()
Usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support.


另外,這裡的this表示的是什麼呢?
一般來說,this表示的是在宣告類別(class)時,
會用到類別自己的成員或是函式時,
用這一個this.myfunction或this.mymember來表示。
但是,目前這一個宣告方式,
我想應該是因為他是一個建構子,
所以,這一個this表示的是一個未來繼承類別使用的。

補充:
var me = this;
我一開始有把這一行指令省略掉,
並且把下面所有me改為this

function LoopProcessor(num) {
    // create a new object for current class
    var me = this;
 
    setTimeout(function () {
 
        for (var i = 1; i <= num; i++) {
            me.emit('BeforeProcess', i);
 
            console.log('Processing number:' + i);
 
            me.emit('AfterProcess', i);
        }
    }
    , 2000)
 
    return this;
}

但這樣是行不通的,
程式跑到那裡會掛掉,
原因是什麼可能還要再研究一下~
var me = this;
是應該會宣告記憶體空間的地方,
所以,不能省略。

參考一下這個網站
Understanding the “this” Keyword in JavaScript
裡面提到
“this” Refers to a New Instance
When a function is invoked with the new keyword, then the function is known as a constructor function and returns a new instance. In such cases, the value of this refers to a newly created instance.
換句話說,若用這個方式的話,看起來是在function使用this
但實際上是建立一個類別(class)
這個方式還蠻特別的


如果我不使用parents的函式的話,
確實是可以不用宣告me這一個變數
例如修改如下

var emitter = require('events').EventEmitter;
// need util module when using inherits
var util = require('util');
 
function LoopProcessor(num) {
    // create a new object for current class
    // var me = this;
    setTimeout(function () {
        for (var i = 1; i <= num; i++) {
            // me.emit('BeforeProcess', i);
            console.log('Processing number:' + i);
            // me.emit('AfterProcess', i);
        }
    }
    , 2000)
    // return this;
}

//LoopProcessor is inherited from emitter class 
util.inherits(LoopProcessor, emitter)
 
var lp = new LoopProcessor(3);
 
lp.on('BeforeProcess', function (data) {
    console.log('About to start the process for ' + data);
});
 
lp.on('AfterProcess', function (data) {
    console.log('Completed processing ' + data);
});

換句話說,
如果我要使用母函式的東西的話,
我在函式宣告的時候,
就必需宣告一個變數,
並且把this指定給它

2020年3月1日 星期日

Node.JS - 簡化函式宣告

目前在學習Node.JS
有一些地方看不太懂,
多看一些網站,總算是了解了,
原來是省略了一些部分,
不過,這樣的寫法可讀性應該會變差,
但是,還是必需要了解別人是在寫什麼。


原本的程式碼

// get the reference of EventEmitter class of events module
var events = require('events');
// create an object of EventEmitter class by using above reference.
var em = new events.EventEmitter();

// bind the function with a event
em.on('FirstEvent', function (data) {
 console.log('First subscriber:' + data);
});
// emit event
em.emit('FirstEvent', 'I have entered the first Event!!');

實際上可以寫的清楚一點如下,
可讀性比較高


// get the reference of EventEmitter class of events module
var events = require('events');
//create an object of EventEmitter class by using above reference.
var em = new events.EventEmitter();

// Create an event handler:
var myEventHandler = function (data) {
 console.log('First subscriber:' + data);
}
// bind the function with a event
em.on('FirstEvent', myEventHandler);

//bind the function with a event
//em.on('FirstEvent', function (data) {
// console.log('First subscriber:' + data);
//});
//emit event
em.emit('FirstEvent', 'I have entered the first Event!!');

這個寫法必需懂,
因為看起來很多Node.JS都會用這個寫法!!

參考資料:
Node.js Events
Day11 - Node.js EventEmitter

2019年8月5日 星期一

如何建立文章縮圖


最近在研究如何建立文章縮圖

參考資料:
https://www.wfublog.com/2015/09/blogger-post-cover-first-image.html

2019年5月15日 星期三

開始學習Python

參考資料: 一小時Python入門-part 1 如何安裝 Jupyter (Ipython Notebook) - start(2019/05/17) - done(2019/05/17)

2019年5月10日 星期五

如何在iframe上可以做link的動作

目前正在研究 或許可以參考 using iframe image as a link

2019年5月8日 星期三

發現有許多template for Blogger

最近在找可以呈現投影片(slideshow)與影片的方式,
本來打算自己架一個wordpress server,
但是,
最後發現Blogger網路上就有許多資源,而且自己還不用架站,
影片就放在youtube再導到Blogger,
圖片放在google drive

目前找到一個喜歡的是
https://btemplates.com/2016/blogger-template-superhero/demo/

最後發現下面這一個更符合我的需求~
因為它的slider可以有兩個描述
而且最後我還發現這個slider可以超連結
https://btemplates.com/2016/blogger-template-healthyliving/demo/

接著我想要讓這一些slider可以直接讀取google drive的圖檔
這個就可以參考下面這一個影片的教學
Embed videos from Google Photos or Drive on your blog or website


發現一個更簡單可以加入slideshow的方式,
而且下面的文章可以直接引用facebook,
並且可以直接預覽
https://bloggerslider.shuvojitdas.com/2015/12/how-to-add-responsive-slideshow-widget-to-blogger.html

來試試看~~

簡單的slideshow
https://www.nosegraze.com/how-to-add-an-image-slider-to-your-blogger-blog/

參考:
https://btemplates.com/
Embed videos from Google Photos or Drive on your blog or website
http://imagesliderforblogger.blogspot.com/

search article by multiple labels

為了方便尋找文章,
通常會在文章後面加入label或tags
但是,如何設定,可以讓我們尋找多個條件的文章呢?

只要在blogger左上角搜尋的欄位輸入多個要尋找的標



Reference:
Blogger multiple label search

2018年8月5日 星期日

只複製資料夾,而不複製裡面的檔案

這個功能使用windows內建的xcopy就可以完成了

xcopy c:\來源資料夾 d:\目的資料夾\ /t/e


參考資料: 如何複製資料夾而不複製資料夾中的檔案

2018年7月18日 星期三

透過QNAP的虛擬主機功能架設多個網站

最近因為工作需要,
必需要學習PHP程式語言,
剛好之前買了一台QNAP,
可以拿來架PHP Server,
又剛好發現它有一個虛擬主機功能,
使用 QNAP NAS 虛擬主機功能架設多個網站

因此很貪心的想在上面架多個網站,
例如:
Joomla, WordPress和phpBB3

因為目前架在區域網路裡面,
同時使用windows 7,
因此最簡單的方法就是去修改
C:\Windows\System32\drivers\etc\hosts 這一個檔案~

一如預期的成功了。
但是,這個方法換個電腦就沒有效了,
因此暝生了在QNAP上架一個DNS Server,
印象中QNAP之前好像有一個DNS的套件,
現在去找竟然找不到。
還好在網路上還是有看到其它方式可以在QNAP上架DNS Server
DNS SERVER ON QNAP-TS-231.

看完之後,
覺得有一些細節還是不太清楚是什麼意思,
因此,同時也參考了
DNS 伺服器的詳細設定

這之中要透過putty連到QNAP NAS,
很簡單,就直接連而已。

最後的網路架構是
Router的DNS設定為QNAP NAS的IP位址(內部網路),
QNAP NAS的上層DNS為Google DNS(8.8.8.8 and 8.8.4.4)

然後,當我在家的裝置透過WIFI或實體網路線取得的網路設定,
其DNS就會是Router's IP address。

用例子來說明,
若我要查詢:
site2.mysite.com

會先跟router's查詢,
不過,我想router應該只是bypass給QNAP DNS Server,
若在QNAP DNS Server有查到,
就會直接回傳IP位址,
若沒有查到的話,
會再傳給Google DNS查詢。

/opt/etc/bind/db.dns
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     AdaCaspar. admin.AdaCaspar. (
                              1         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      localhost.
@       IN      A       127.0.0.1

;
; Add customer DNS setting
;
phplearning     IN      A       192.168.20.25
joomlacms       IN      A       192.168.20.25
phpbb3          IN      A       192.168.20.25
wordpress       IN      A       192.168.20.25

Reference: DNS SERVER ON QNAP-TS-231

2017年7月15日 星期六

SUBST指令

Associates a path with a drive letter.

SUBST [drive1: [drive2:]path]
SUBST drive1: /D

  drive1:        Specifies a virtual drive to which you want to assign a path.
  [drive2:]path  Specifies a physical drive and path you want to assign to
                 a virtual drive.
  /D             Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives.

參考資料:
Show Google Drive in Windows as a Local Hard Drive
Microsoft Windows Subst command
Make A Virtual Drive

mklink指令

Creates a symbolic link.

MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      Creates a directory symbolic link.  Default is a file
                symbolic link.
        /H      Creates a hard link instead of a symbolic link.
        /J      Creates a Directory Junction.
        Link    specifies the new symbolic link name.
        Target  specifies the path (relative or absolute) that the new link
                refers to.

2017年6月24日 星期六

逐字稿工具

為了訓練自己的英文
現在要把上課的內容用逐字稿的方式打出來,
希望對自己的英文有所進步。

參考資料:
協同逐字稿
oTranscribe

2017年6月5日 星期一

vim中的零寬度匹配

最近發現vim有一個功能
叫做「零寬度匹配」
還蠻繞口的
還搞不太懂,
先記錄一下吧~

參考資料:
[转]vim中的零宽度匹配 - [1.2 vim]
VIM REFERENCE MANUAL