Skip to content
Browse files

v0.5.8 Added United Test and fixed some bugs

  • Loading branch information...
2 parents 5d592f2 + 0229c6c commit a67fbb9790b11b89e2c5a8cb6600bf0b614ecc97 @iwillwen committed Aug 3, 2012
Showing with 155 additions and 1 deletion.
  1. +130 −1 lib/method.js
  2. +14 −0 lib/request.js
  3. +11 −0 lib/response.js
View
131 lib/method.js
@@ -489,7 +489,6 @@ function method (web) {
return server;
}
};
-
web.dynamicHelpers = function (obj) {
var self = this;
return self.use(function (req, res, next) {
@@ -500,6 +499,136 @@ function method (web) {
next();
});
};
+ web.ext = function (name, handle) {
+ web.__defineGetter__(name, function () {
+ return handle
+ });
+ };
+ /*
+ * @description 设置一些需要用到的元数据
+ * @param {String} key 元数据的Key*
+ * @param {String} value 元数据的值*
+ */
+ web.metas = {};
+ web.config = function (key, value) {
+ var self = this;
+ switch (typeof key) {
+ case 'string':
+ if (value !== undefined) {
+ switch (key) {
+ case 'template':
+ web.render = function (tmlp, view, callback) {
+ if (web.set('views') === undefined) tmlp = web.set('views') + '/' + tmlp;
+ var engine = web.set('view engine') || require('jade');
+ fs.readFile(tmlp, function (err, data) {
+ engine.render(data, view, callback);
+ });
+ };
+ break;
+ case 'mode':
+ switch (value) {
+ case 'production':
+ case 'pro':
+ setInterval(function () {
+ web.restart();
+ }, 31536000);
+ web.metas[key] = value;
+ break;
+ case 'development':
+ case 'dev':
+ web.use(web.logger('dev'));
+ global.web.metas[key] = value;
+ break;
+ }
+ break;
+ default:
+ self.metas[key] = value;
+ global.web.metas[key] = value;
+ self.emit('set', key, value);
+ self.emit('config', key, value);
+ global.web.emit('set', key, value);
+ global.web.emit('config', key, value);
+ }
+ } else {
+ if (self.metas[key])
+ return self.metas[key]
+ else
+ return global.web.metas[key];
+ }
+ break;
+ case 'object':
+ for (var meta in key) {
+ self.metas[key] = value;
+ global.web.set(meta, key[meta]);
+ }
+ break;
+ }
+ return this;
+ };
+ web.set = web.config;
+ web.meta = web.set;
+ /*
+ * @description 重启服务器
+ * @param {Object} server 服务器
+ */
+ web.restart = function (server) {
+ if (server) {
+ server.self.close();
+ server.self.listen(server.port, server.host);
+ } else {
+ web.server.self.close();
+ web.server.self.listen(web.server.port, web.server.host);
+ }
+ return this;
+ };
+ /*
+ * @description 停止服务器
+ * @param {Object} server 服务器
+ */
+ web.stop = function(server){
+ server = server ? server : web.server;
+ if(server.fd)
+ server.self.close();
+ return this;
+ };
+ /*
+ * @description 生成一个服务器
+ * @param {String} type 服务器的类型*
+ * @param {Object} opt HTTPS服务器的SSL设置
+ */
+ web.create = function (type, opt) {
+ switch (type) {
+ case 'http':
+ var server = connect.createServer();
+ web.server = server;
+ global.server = server;
+ server.isServer = true;
+ exports.ext(server);
+ return server;
+ break;
+ case 'https':
+ var server = connect.createServer();
+ var httpsServer = https.createServer(opt, server);
+ server.setRouter = false;
+ server.on('listening', function (port, host) {
+ httpsServer.listen(port, host);
+ server.self = httpServer;
+ });
+ web.httpsServer = server;
+ global.httpsServer = server;
+ server.isServer = true;
+ exports.ext(server);
+ return server;
+ break;
+ default:
+ var server = connect.createServer();
+ web.server = server;
+ global.server = server;
+ server.isServer = true;
+ exports.ext(server);
+ return server;
+ }
+ };
web.render = function () {
var self = this;
View
14 lib/request.js
@@ -15,6 +15,7 @@ var async = process.nextTick;
exports = module.exports = Request;
function Request (req) {
+<<<<<<< HEAD
var source = new dataStream();
utils.merge(this, source);
req.pipe(this);
@@ -26,6 +27,19 @@ function Request (req) {
Object.defineProperty(this, 'connecttion', { get: function () { return req.connecttion; } });
Object.defineProperty(this, 'self', { get: function () { return req; } });
for (var key in req) if (navites.indexOf(key) === -1) this[key] = req[key];
+=======
+ var source = new dataStream();
+ utils.merge(this, source);
+ req.pipe(this);
+ this.url = req.url;
+ this.method = req.method;
+ this.headers = req.headers;
+ this.trailers = req.trailers;
+ this.httpVersion = req.httpVersion;
+ Object.defineProperty(this, 'connecttion', { get: function () { return req.connecttion; } });
+ Object.defineProperty(this, 'self', { get: function () { return req; } });
+ for (var key in req) if (navites.indexOf(key) === -1) this[key] = req[key];
+>>>>>>> 0229c6ca769f2a7111c5a24df3efc5fa12f26f78
}
util.inherits(Request, dataStream);
View
11 lib/response.js
@@ -32,6 +32,7 @@ function Response (res, req) {
// force merge
utils.merge(this, source);
+<<<<<<< HEAD
// let the new response object can be use as a navite response object
this.statusCode = res.statusCode;
this.self = res;
@@ -41,6 +42,16 @@ function Response (res, req) {
this.removeHeader = function () {res.removeHeader.apply(res, arguments);};
this.addTrailers = function () {res.addTrailers.apply(res, arguments);};
for (var key in res) if (navites.indexOf(key) === -1) this[key] = res[key];
+=======
+ // let the new response object can be use as a navite response object
+ this.statusCode = res.statusCode;
+ Object.defineProperty(this, 'self', { get: function () { return res; } });
+ this.getHeader = function () {res.getHeader.apply(res, arguments);};
+ this.setHeader = function () {res.setHeader.apply(res, arguments);};
+ this.removeHeader = function () {res.removeHeader.apply(res, arguments);};
+ this.addTrailers = function () {res.addTrailers.apply(res, arguments);};
+ for (var key in res) if (navites.indexOf(key) === -1) this[key] = res[key];
+>>>>>>> 0229c6ca769f2a7111c5a24df3efc5fa12f26f78
}
util.inherits(Response, dataStream);

0 comments on commit a67fbb9

Please sign in to comment.
Something went wrong with that request. Please try again.