學習了connect modulejava
nodejs 的中間件方式node
var connect = require('connect'); var server = connect.createServer();
// connect.logger 是一箇中間件 server.use(connect.logger('dev'));
// 值得學習的是 next 方法 server.use(function(req, res, next) { if ('/a' == req.url) { // here do something } else { // next is the require chain like java filter china next(); } });
server.listen(3000);
connect 的中間件,還包括:web
connect.static // 映射靜態路徑 cookie
// web root path server.use(connect.static(__dirname + '/resource')); // path '/images' point to '/path/to/images' server.use('/images', connect.static('/path/to/images')); // client side cache server.use('/js', connect.static('/path/to/bounds', { maxAge: 10000000 }); // hidden server.use(connect.static('/path/to/resources', { hidden: true });
connect.query // 解析GET參數session
connect.bodyParser // 解析POST參數app
connect.logger // 日誌ide
connect.cookieParser // 解析cookie學習
connect.session // sessionui
var server = connect( connect.logger('dev'), connect.bodyParser(), connect.cookieParser(), connect.session({secret: 'my app secret'}), function(req, res, next) { if (req.url == '/images') { // todo ... } else { next(); } }
);
nodejs的代碼真是簡潔啊~url