開啟防火牆
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
技巧建立在基礎之上
開啟防火牆
sudo ufw enable
sudo ufw allow mysql
sudo ufw allow ssh
sudo ufw allow 22
最近要安裝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
在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
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
在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);
});
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 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);
});
目前在學習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!!');