iT邦幫忙

DAY 21
9

node.js伺服器實戰系列 第 21

node.js伺服器實戰(21) - 建構伺服器流程

接下來,就要實際調整伺服器程式,讓它可以支援前一天規劃的流程構想。
插入點

舊的程式(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。

相關文章

(今天早上進了急診室,還好晚上出來了,來得及寫這篇XD,只是我怕考慮不夠周詳就是了...)


上一篇
node.js伺服器實戰(20) - 伺服器架構設計
下一篇
Node.js伺服器實戰(22) - 細部調整
系列文
node.js伺服器實戰33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
wordsmith
iT邦高手 1 級 ‧ 2011-10-31 23:09:22

好好保重身體啊,鐵人賽要顧,身體更要顧。

jamesjan iT邦高手 1 級 ‧ 2011-11-01 08:55:38 檢舉

Hold 住費大!God bless you.

總裁 iT邦好手 1 級 ‧ 2011-11-01 10:42:15 檢舉

抱抱費大要保重呀!!!

我要留言

立即登入留言