Permalink
Browse files

Fixed response.sendError

  • Loading branch information...
1 parent 89e7c2b commit 1dc3aa211ff0dfd7c4e19e90e595834414c850cb @iwillwen committed Dec 9, 2012
Showing with 69 additions and 46 deletions.
  1. +69 −46 lib/response.js
View
@@ -11,13 +11,12 @@ var mimes = require('./mimes').mimes;
var util = require('util');
var utils = require('./utils');
var dataStream = require('dataStream');
-var httpStatus = require('./httpstatus').status;
var eventproxy = require('eventproxy').EventProxy;
var http = require('http');
// deafult cookie config
var expires = {
- maxAge: 60*60*24*365
+ maxAge: 60 * 60 * 24 * 365
};
exports = module.exports = Response;
@@ -34,10 +33,10 @@ function Response (res, req) {
this.statusCode = res.statusCode;
this.self = res;
this.callback = (req.query ? req.query.callback : null);
- 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);};
+ 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];
}
@@ -65,19 +64,21 @@ util.inherits(Response, dataStream);
* You only call the method once in a router handler if you have not call the long method.
*
*/
-Response.prototype.send = function (data, _charset) {
+Response.prototype.send = function(data, _charset) {
// response object
var self = this;
// emit 'send' event
this.emit('send');
+ this.text = true;
+
// set the response complete
this.statusCode = 200;
// set the content type with UTF-8 encoded.
- var charset = _charset || 'text/html; charset=UTF-8';
+ var charset = _charset || 'text/html; charset=utf-8';
// headers
this.header('Content-Type', charset);
@@ -93,7 +94,7 @@ Response.prototype.send = function (data, _charset) {
// begin setting the stream pipelining
var last = this;
- pipes.push(function () {
+ pipes.push(function() {
return self.self;
});
for (var i = 0; i < pipes.length; i++) {
@@ -119,7 +120,7 @@ Response.prototype.send = function (data, _charset) {
* .send('')
*
*/
-Response.prototype.long = function () {
+Response.prototype.long = function() {
// response object
var self = this;
@@ -134,7 +135,7 @@ Response.prototype.long = function () {
// begin setting the stream pipelining
var last = this;
- pipes.push(function () {
+ pipes.push(function() {
return self.self;
});
for (var i = 0; i < pipes.length; i++) {
@@ -151,14 +152,14 @@ Response.prototype.long = function () {
this.header('X-Server', 'webjs');
// reset the send method
- this.send = function (data) {
+ this.send = function(data) {
this.write(data);
return this;
};
this._end = this.end;
- this.end = function (charset) {
+ this.end = function(charset) {
// set the content type with UTF-8 encoded.
- charset = charset || 'text/html; charset=UTF-8';
+ charset = charset || 'text/html; charset=utf-8';
this.header('Content-Type', charset);
return this._end();
@@ -178,7 +179,7 @@ Response.prototype.long = function () {
* .('Sorry! The page not found!');
*
*/
-Response.prototype.status = function (code) {
+Response.prototype.status = function(code) {
// emit the status event
this.emit('status');
@@ -202,7 +203,7 @@ Response.prototype.status = function (code) {
* res.cache('no-cache');
*
*/
-Response.prototype.cache = function (type, options) {
+Response.prototype.cache = function(type, options) {
// emit the cache event
this.emit('cache');
@@ -240,7 +241,7 @@ Response.prototype.download = Response.prototype.attachment;
* 'view engine': 'ejs',
* 'ejs': 'html'
* })
- * .get('/foo', function (req, res) {
+ * .get('/foo', function(req, res) {
* res.render('test', {
* title: 'Test Template Render',
* local: {
@@ -250,7 +251,7 @@ Response.prototype.download = Response.prototype.attachment;
* });
*
*/
-Response.prototype.render = function () {
+Response.prototype.render = function() {
// copy the response object
var self = this;
@@ -311,12 +312,12 @@ Response.prototype.render = function () {
// set up async assgin
- render.assign('layout', 'body', function (layout, body) {
+ render.assign('layout', 'body', function(layout, body) {
// view object
layout_view.body = body;
if (layout) {
- var fin = Render(layout, layout_view, function (err, fin) {
+ var fin = Render(layout, layout_view, function(err, fin) {
// fallback
if (err) return self.sendError(500);
@@ -339,25 +340,25 @@ Response.prototype.render = function () {
// deal with the layout
if (opt.layout !== false) {
if (opt.layout === undefined || opt.layout === true) {
- fs.readFile(root + 'layout.' + ext, function (err, layout) {
+ fs.readFile(root + 'layout.' + ext, function(err, layout) {
if (err) return render.trigger('layout', '!= body');
render.trigger('layout', layout.toString());
});
} else {
- fs.readFile(root + opt.layout + '.' + ext, function (err, layout) {
+ fs.readFile(root + opt.layout + '.' + ext, function(err, layout) {
if (err) return render.trigger('layout', '!= body');
render.trigger('layout', layout.toString());
});
}
} else {
render.trigger('layout', false);
}
- fs.readFile(root + name + '.' + ext, function (err, tmlp) {
+ fs.readFile(root + name + '.' + ext, function(err, tmlp) {
// fallback
if (err) return self.sendError(500);
// rende
- var body = Render(tmlp.toString(), view, function (err, body) {
+ var body = Render(tmlp.toString(), view, function(err, body) {
// fallback
if (err) return self.sendError(500);
@@ -376,39 +377,61 @@ Response.prototype.render = function () {
* res.sendError(404);
*
*/
-Response.prototype.sendError = function (statu) {
+Response.prototype.sendError = function() {
+
+ var args = Array.prototype.slice.call(arguments);
+
+ var statu = args[0];
+ var argv = args.slice(1);
+
+ if (util.isError(statu)) {
+ this.self.statusCode = 503;
+ this.header('Content-Type', 'text/html');
+ return this.self.end('Error: ' + statu.message);
+ }
// emit http_error event
this.emit('http_error', statu);
- var errHandler = global.web.server.handlers.error[statu] || null;
+ var errHandler = (global.web.server.handlers.error ? global.web.server.handlers.error[statu] : null);
if (errHandler) {
var req = new http.IncomingMessage(this.self.connection);
- errHandler(req, this, function () {
- // fetch the error data
- var data = global.web.ErrorPage[statu] ? global.web.ErrorPage[statu] : httpStatus[String(statu)];
+ if (argv.length < 1) {
+ errHandler(req, this, function() {
+ // fetch the error data
+ var data = global.web.ErrorPage[statu] ? global.web.ErrorPage[statu] : http.STATUS_CODES[statu.toString()];
+
+ this.self.statusCode = statu;
+ this.header('Content-Type', 'text/html');
+ this.self.end(data);
+ });
+ } else {
+ errHandler(argv[0], req, this, function() {
+ // fetch the error data
+ var data = global.web.ErrorPage[statu] ? global.web.ErrorPage[statu] : http.STATUS_CODES[statu.toString()];
- this.statusCode = statu;
- this.header('Content-Type', 'text/html');
- this.end(data);
- });
+ this.self.statusCode = statu;
+ this.header('Content-Type', 'text/html');
+ this.self.end(data);
+ });
+ }
return this;
}
// fetch the error data
- var data = global.web.ErrorPage[statu] ? global.web.ErrorPage[statu] : httpStatus[String(statu)];
+ var data = (global.web.ErrorPage && global.web.ErrorPage[statu] ? global.web.ErrorPage[statu] : http.STATUS_CODES[statu.toString()]);
this.statusCode = statu;
this.header('Content-Type', 'text/html');
- this.end(data);
+ this.self.end(data);
return this;
};
/*
* @description Send a file to client. 发送指定文件到客户端
* @param {String} fileName Specify file name to send.(require) 需要发送的文件的文件名(不包括文件名前端的'/');*
*/
-Response.prototype.sendFile = function (fileName, found, _charset) {
+Response.prototype.sendFile = function(fileName, found, _charset) {
this.emit('sendfile', fileName);
if (!found) {
var _server = typeof server == 'object' ? server : global.web.server;
@@ -432,7 +455,7 @@ Response.prototype.sendfile = Response.prototype.sendFile;
* @description Send a JSON String to client. 发送JSON数据到客户端
* @param {Array} data A data to send, it can be Array, Object or String.(require) 需要发送的数据,可以是Array, Object或是已经编码的JSON字符串*
*/
-Response.prototype.sendJSON = function (data) {
+Response.prototype.sendJSON = function(data) {
this.emit('sendJSON', data);
switch (typeof data) {
case "string":
@@ -450,7 +473,7 @@ Response.prototype.json = Response.prototype.sendJSON;
* @description Send a JSON String to client, and then run the callback. 发送JSONP数据到客户端,然后让客户端执行回调函数。
* @param {Array} data A data to send, it can be Array, Object or String.(require) 需要发送的数据,可以是Array, Object或是已经编码的JSON字符串*
*/
-Response.prototype.sendJSONP = function (data) {
+Response.prototype.sendJSONP = function(data) {
this.emit('sendJSONP', data);
var callback = this.callback;
switch (typeof data) {
@@ -469,7 +492,7 @@ Response.prototype.jsonp = Response.prototype.sendJSONP;
* @description Redirect the client to specify url ,home, back or refresh. 使客户端重定向到指定域名,或者重定向到首页,返回上一页,刷新。
* @param {String} url Specify url ,home, back or refresh.(require) 指定的域名,首页,返回或刷新。*
*/
-Response.prototype.redirect = function (url) {
+Response.prototype.redirect = function(url) {
this.emit('redirect', url);
this.self.statusCode = 302;
this.header('Location', url);
@@ -483,7 +506,7 @@ Response.prototype.redirect = function (url) {
* @param {String} val content of the cookies.(require) cookies的数据*
* @param {Object} options Detail options of the cookies. cookies的详细设置
*/
-Response.prototype.setCookie = function (name, val, options) {
+Response.prototype.setCookie = function(name, val, options) {
this.emit('setCookie', name, val);
if (typeof options != 'object')
options = {};
@@ -507,15 +530,15 @@ Response.prototype.cookie = Response.prototype.setCookie;
* @param {String} name Name of the cookies to clear.(require) 需要清除的cookies的名字*
* @param {Object} options Detail options of the cookies. cookies的详细设置
*/
-Response.prototype.clearCookie = function (name, options) {
+Response.prototype.clearCookie = function(name, options) {
this.cookie(name, '', options);
return this;
};
-Response.prototype.header = function (header, value) {
+Response.prototype.header = function(header, value) {
this.emit('header', header, value);
- if (value && !this._headerSent) this.setHeader(header, value);
- if (value === false && !this._headerSent) return this.removeHeader(header);
- if (!value) return this.getHeader(header);
+ if (value && !this.self._headerSent) this.self.setHeader(header, value);
+ if (value === false && !this.self._headerSent) return this.self.removeHeader(header);
+ if (!value) return this.self.getHeader(header);
return this;
};
@@ -524,7 +547,7 @@ Response.prototype.header = function (header, value) {
function sendfile (_fileName, res, found, _charset) {
if (!found) if (/^\//.test(_fileName)) _fileName = _fileName.substr(1);
- fs.stat(_fileName, function (err, stats) {
+ fs.stat(_fileName, function(err, stats) {
if (err) return res.sendError(404);
var size = stats.size;
var format = require('path').extname(_fileName);

0 comments on commit 1dc3aa2

Please sign in to comment.