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指定給它