http中間件:connect(sencha實驗室的產品) 提供一個類似javaee過濾器的框架,提供了很多中間件,如:日志、靜態文件服務器,seesion等功能,官網 https://github.com/senchalabs/connect/ ,使用或看源碼推薦看 https://github.com/senchalabs/connect/tree/1.8.4,因為默認首頁是2.0版,代碼已經重寫了,幫助文檔 http://senchalabs.github.com/connect/。
相關閱讀:
http://www.linuxidc.com/Linux/2012-02/53530.htm
http://www.linuxidc.com/Linux/2012-02/53531.htm
開始安裝輸入npm install connect,要等一會,再輸入npm list
安裝成功。該目錄下會生成node_modules文件夾。
有位仁兄對nodejs模塊connect源碼進行分析,鏈接http://www.linuxidc.com/Linux/2012-02/53529.htm
我也看了一下源碼,不是太復雜,主要是利用javascript的動態語音特性,核心代碼片段如下:
connect.js 代碼片段,對http進行封裝
[javascript]
- var HTTPServer = require('./http').Server
-
- // expose createServer() as the module
- exports = module.exports = createServer;
-
- function createServer() {
- if ('object' == typeof arguments[0]) {
- return new HTTPSServer(arguments[0], Array.prototype.slice.call(arguments, 1));
- } else {
- return new HTTPServer(Array.prototype.slice.call(arguments));
- }
- };
-
- fs.readdirSync(__dirname + '/middleware').forEach(function(filename){
- if (/\.js$/.test(filename)) {
- var name = filename.substr(0, filename.lastIndexOf('.'));
- exports.middleware.__defineGetter__(name, function(){
- return require('./middleware/' + name);
- });
- }
- });
-
- exports.utils.merge(exports, exports.middleware);
http.js 代碼片段,this.stack 存儲了所有的過濾鏈表,真的非常想java的過濾器,中間件的參數很像java的FilterConfig, next()很像java的 filterChain.doFilter,可能更像Struts2 提供的默認攔截器棧
[javascript]
- var Server = exports.Server = function HTTPServer(middleware) {
- this.stack = [];
- middleware.forEach(function(fn){
- this.use(fn);
- }, this);
- http.Server.call(this, this.handle);
- };
-
- Server.prototype.__proto__ = http.Server.prototype;
-
- Server.prototype.use = function(route, handle){
- ...
- this.stack.push({ route: route, handle: handle });
-
- // allow chaining
- return this;
- };
-
-
- Server.prototype.handle = function(req, res, out) {
- ...
- function next(err) {
- ...
- layer = stack[index++];
- ...
- var arity = layer.handle.length;
- if (err) {
- if (arity === 4) {
- layer.handle(err, req, res, next);
- } else {
- next(err);
- }
- } else if (arity < 4) {
- layer.handle(req, res, next);
- } else {
- next();
- }
- ...
- next();
- }
看了最簡單的中間件 middleware/query.js,基本就明白,使用js動態語音特性,動態賦屬性。
[javascript]
- var qs = require('qs')
- , parse = require('url').parse;
-
- module.exports = function query(){
- return function query(req, res, next){
- req.query = ~req.url.indexOf('?')
- ? qs.parse(parse(req.url).query)
- : {};
- next();
- };
- };
http.Server.call(this, this.handle); 開始沒看懂 ,後來看了 node.js 原本的http.js的代碼片段
node.js,知道什麼意思了
[javascript]
- exports.createServer = function(requestListener) {
- return new Server(requestListener);
- };
-
- function Server(requestListener)
寫一個簡單的實例 hello world
代碼如下,保存為 connectTest.js,放在敲安裝命令 的目錄下:
[javascript]
- var connect = require('connect');
-
- var app = connect()
- .use(connect.query())
- .use(function(req, res){
- var name = req.query.name;
- res.end("hello " + name);
- });
-
- app.listen(8080);
- console.log('Server started on port 8080');
命令行輸入 node connectTest.js
在浏覽器地址欄輸入 http://localhost:8080/?name=zhang,結果如下
就寫到這,後面再寫一些connect的應用。