接下來,就要實際調整伺服器程式,讓它可以支援前一天規劃的流程構想。
插入點
舊的程式(evolve.js):
var http = require('http'),
tools = require('./tools');
var count = 0;
module.exports = function(conf) {
var server = http.createServer(function(request, response) {
//這裡是pre dispatch hook
tools.cookieHandler(request, response);
console.log(process.pid + '::' + (++count) + ': ' + process.memoryUsage().heapUsed);
//這裡是dispatch hook
tools.dispatch(conf, request, response, function(err, realpath, data) {
if(err) {
console.log(process.pid + '::' + realpath + ' not exists.');
response.writeHead(404, {"Content-Type":"text/html"});
response.end('<h1>404: Request resource not found.</h1>');
} else {
console.log(process.pid + '::' + realpath + ' exists.');
response.writeHead(200, {
"Content-Type": data.type,
"Content-Length": data.data.length
});
response.end(data.data);
}
});
//這裡是post dipatch hook
});
this.listen = function(port, addr) {
server.listen(port, addr);
return this;
};
this.close = function() {
server.close();
return this;
};
this.host = tools.hostHandler;
};
需要的插入點有三個,分別是pre dispatch、dispatch、post dispatch,大致上位於上面的註解中的位置。tools.cookieHandler以及tools.dispatch則需要因應架構來調整位置。另外,需要有一個函數(方法)來加入這些handler。
實做
經過調整以後,程式變成:
var http = require('http'),
tools = require('./tools');
var count = 0;
module.exports = function(conf) {
var server = http.createServer(function(request, response) {
var dispatched = false;
console.log(process.pid + '::' + (++count) + ': ' + process.memoryUsage().heapUsed);
this.emit('pre', request, response);
this.emit('dispatch', conf, request, response, function(err, realpath, data) {
if(dispatched) {
return;
} else {
dispatched = true;
}
if(err) {
console.log(process.pid + '::' + realpath + ' not exists.');
response.writeHead(404, {"Content-Type":"text/html"});
response.end('<h1>404: Request resource not found.</h1>');
} else {
console.log(process.pid + '::' + realpath + ' exists.');
response.writeHead(200, {
"Content-Type": data.type,
"Content-Length": data.data.length
});
response.end(data.data);
}
});
this.emit('post', request, response);
});
this.listen = function(port, addr) {
server.listen(port, addr);
return this;
};
this.close = function() {
server.close();
return this;
};
this.host = tools.hostHandler;
this.handle = function(hook, handler) {
server.on(hook, handler);
};
this.handle('dispatch', tools.dispatch);
};
基本上很簡單,就是使用三個事件來實做,不過為了讓dispatcher只執行一次,稍微加了工。為了讓事件名稱不要太長,把他們改成:pre、dispatch、post三個。另外,cookieHandler則需要調整位置...例如:
var Evolve = require('../lib/evolve');
var tools = require('../lib/tools');
var path = require('path');
var server = new Evolve({
dirindex: ['index.html', 'index.htm', 'default.htm']
});
server.handle('pre', tools.cookieHandler);
server
.host('localhost:8443')
.map('/', path.join(__dirname, '../www'))
.get('/hello', function (request, response, cb) {
cb(false, '/hello', {
type: 'text/html',
data: 'hello'
});
});
server.listen(8443, 'localhost');
利用handle方法,來增加pre規則,在這裡處理cookie。整合測試需要一併修改,例如testCookie.js需要用新的分法加入cookie handler。修改以後,就可以順利通過整合測試。
今天的範例檔fillano-evolve-v0.0.15-0-ga38cc1c.zip,目前版本是v0.0.15。
(今天早上進了急診室,還好晚上出來了,來得及寫這篇,只是我怕考慮不夠周詳就是了...)
好好保重身體啊,鐵人賽要顧,身體更要顧。
Hold 住費大!God bless you.
費大要保重呀!!!