diff --git a/lib/server.js b/lib/server.js index b5dc45c1..3ac8cc5e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -67,14 +67,17 @@ function createServer(port, ip, protocol, callback) { var server, app, respondApp, - middle = controller.middle(DIR), - isOption = function(name) { return config(name); }, isMinify = isOption.bind(null, 'minify'), isOnline = isOption.bind(null, 'online'), + isCache = isOption.bind(null, 'cache'), + + ponseStatic = ponse.static(DIR, { + cache: isCache + }), funcs = [ rest, @@ -101,9 +104,9 @@ if (expressApp) { app = expressApp; - app.use(middle); + app.use(ponseStatic); } else { - funcs.push(controller.middle(DIR)); + funcs.push(ponseStatic); respondApp = Util.exec.with(respond, funcs); app = respondApp; @@ -157,34 +160,4 @@ Util.exec.series(funcs); } - /** - * Главная функция, через которую проихсодит - * взаимодействие, обмен данными с клиентом - * @param req - запрос клиента (Request) - * @param res - ответ сервера (Response) - */ - function controller(dir, req, res) { - var parsedUrl = URL.parse(req.url), - query = parsedUrl.search || '', - name = ponse.getPathName(req); - - if (!expressApp) - Util.log(req.method + ' ' + name + query); - - name = path.join(dir, name); - - ponse.sendFile({ - name : name, - cache : config('cache'), - gzip : true, - request : req, - response : res - }); - - } - - controller.middle = function(dir) { - return controller.bind(null, dir); - }; - })(); diff --git a/lib/server/ponse.js b/lib/server/ponse.js index 2aafa655..df536097 100644 --- a/lib/server/ponse.js +++ b/lib/server/ponse.js @@ -4,6 +4,7 @@ var fs = require('fs'), zlib = require('zlib'), url = require('url'), + path = require('path'), querystring = require('querystring'), DIR_JSON = __dirname + '/../../json/', @@ -30,6 +31,10 @@ exports.setHeader = setHeader; + exports.static = function(dir, options) { + return getStatic.bind(null, dir, options); + }; + /* Функция высылает ответ серверу * @param data * @param params @@ -325,4 +330,27 @@ response.end(); } + function getStatic(dir, options, req, res) { + var cache, + o = options || {}, + name = getPathName(req); + + name = path.join(dir, name); + + if (Util.isFunction(o.cache)) + cache = o.cache(); + else if (o.cache !== undefined) + cache = o.cache; + else + cache = true; + + sendFile({ + name : name, + cache : cache, + gzip : true, + request : req, + response : res + }); + } + })();