mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-02-01 15:23:03 +00:00
添加反向代理函数及swig模板支持
This commit is contained in:
parent
f871e836fc
commit
9132b47c89
@ -421,6 +421,51 @@ module.exports = Class(function() {
|
||||
});
|
||||
return deferred.promise;
|
||||
},
|
||||
|
||||
/**
|
||||
* 添加反向代理函数,可以发起向其他服务器的请求
|
||||
* @param {[type]} url [description]
|
||||
* @param {[type]} mode [description]
|
||||
* @return Promise [description]
|
||||
*/
|
||||
proxy: function(url, mode) {
|
||||
var self = this;
|
||||
var deferred = getDefer();
|
||||
var data = '';
|
||||
|
||||
var timeoutSec = APP_DEBUG?1000:3000;
|
||||
|
||||
var req = http.get(url, function(res){
|
||||
res.on('data', function(chunk){
|
||||
data += chunk;
|
||||
});
|
||||
});
|
||||
|
||||
req.on('close', function(){
|
||||
// 如果mode为TRUE,直接输出请求到的内容
|
||||
if(mode === true) {
|
||||
self.end(data);
|
||||
}
|
||||
|
||||
try {
|
||||
data = JSON.parse(data);
|
||||
} catch(e) {
|
||||
data = null;
|
||||
}
|
||||
deferred.resolve(data);
|
||||
});
|
||||
|
||||
// 设置超时时间为3000
|
||||
req.setTimeout(timeoutSec, function(e){
|
||||
deferred.resolve(null);
|
||||
});
|
||||
|
||||
req.on('error', function(err){
|
||||
deferred.resolve(null);
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
}
|
||||
/**
|
||||
* 正常json数据输出
|
||||
* @param {[type]} data [description]
|
||||
|
||||
18
lib/Lib/Driver/Template/SwigTemplate.js
Normal file
18
lib/Lib/Driver/Template/SwigTemplate.js
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* swig
|
||||
* http://paularmstrong.github.io/swig/
|
||||
* @type {[type]}
|
||||
*/
|
||||
var swig = require('swig');
|
||||
|
||||
module.exports = {
|
||||
fetch: function(templateFile, tVar){
|
||||
'use strict';
|
||||
var content = getFileContent(templateFile);
|
||||
var conf = extend({
|
||||
filename: templateFile,
|
||||
cache: true
|
||||
}, C('tpl_engine_config'));
|
||||
return swig.compile(content, conf)(tVar);
|
||||
}
|
||||
};
|
||||
@ -16,6 +16,7 @@
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"swig": "1.4.2",
|
||||
"ejs": "0.8.4",
|
||||
"multiparty": "3.0.0",
|
||||
"es6-promise": "0.1.1",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user