mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-21 10:38:26 +00:00
feature(ponse) add static
This commit is contained in:
parent
f3d9a88da1
commit
9f0b42d468
2 changed files with 35 additions and 34 deletions
|
|
@ -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);
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
});
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue