Skip to content
Browse files

More flexible return from a route function (including status_code and…

… content_type)
  • Loading branch information...
1 parent b307c86 commit 3849ad8194eeec66214cf1d768d0250c5a82da8e @pwlin committed May 18, 2012
Showing with 24 additions and 14 deletions.
  1. +15 −12 lib/ananas/index.js
  2. +9 −2 www-example/localhost.dev/example_dynamic/index.js
View
27 lib/ananas/index.js
@@ -95,8 +95,9 @@ var ananas = {
return false;
}
var status_code = 200;
- var data = '';
- var content_type = '';
+ var content_type = 'text/plain';
+ var data = {} || '';
+ var content = '';
var self = this;
var route_found = false;
if(config['app_status'].toLowerCase() == 'dev') {
@@ -120,7 +121,9 @@ var ananas = {
catch(e) {
console.error("\nAnanas route error:\n", "Route:\n", route + "\n", "Strack trace:\n", e.stack + "\n");
}
- content_type = config['routes'][route]['content_type'];
+ content_type = data['content_type'] || (config['routes'][route]['content_type'] || content_type);
+ status_code = data['status_code'] || (config['routes'][route]['status_code'] || status_code);
+ content = data['content'] || (data || content);
route_found = true;
break;
}
@@ -132,7 +135,7 @@ var ananas = {
'response' : response,
'status_code' : status_code,
'config' : config,
- 'data' : data,
+ 'content' : content,
'content_type' : content_type
});
} else {
@@ -144,7 +147,7 @@ var ananas = {
var filename = '';
var private_uri_regexp_pattern = config['private_uri'] ? new RegExp(config['private_uri'], 'ig') : false;
var status_code = 200;
- var data = '';
+ var content = '';
var content_type = '';
var self = this;
if(config['uri'].match(/\/\.|404\.html$/ig) || (config['private_uri'] && config['uri'].match(private_uri_regexp_pattern))) {
@@ -166,7 +169,7 @@ var ananas = {
'status_code' : status_code,
'stats' : stats,
'config' : config,
- 'data' : self.print_directory_listing({'parent' : filename, 'files' : files, 'config' : config}),
+ 'content' : self.print_directory_listing({'parent' : filename, 'files' : files, 'config' : config}),
'content_type' : 'text/html; charset=utf-8'
});
});
@@ -186,7 +189,7 @@ var ananas = {
'status_code' : 404,
'stats' : false,
'config' : config,
- 'data' : data,
+ 'content' : content,
'content_type' : content_type
});
}
@@ -198,7 +201,7 @@ var ananas = {
'status_code' : status_code,
'stats' : stats,
'config' : config,
- 'data' : data,
+ 'content' : content,
'content_type' : content_type
});
}
@@ -211,7 +214,7 @@ var ananas = {
'status_code' : status_code,
'stats' : stats,
'config' : config,
- 'data' : data,
+ 'content' : content,
'content_type' : content_type
});
});
@@ -282,7 +285,7 @@ var ananas = {
* opts:
* request, response, filename, status_code, stats, config, data, content_Type
*/
- if (opts['data'] != '') {
+ if (opts['content'] != '') {
return this.serve_data(opts);
}
else {
@@ -294,12 +297,12 @@ var ananas = {
var headers = {
'Date' : (new Date()).toUTCString(),
'Server' : opts['config']['server_name'],
- 'Content-Length' : Buffer.byteLength(opts['data'], 'utf8'),
+ 'Content-Length' : Buffer.byteLength(opts['content'], 'utf8'),
'Content-Type' : opts['content_type'],
'Connection' : 'close'
};
opts['response'].writeHead(opts['status_code'], headers);
- opts['response'].write(opts['data']);
+ opts['response'].write(opts['content']);
opts['response'].end();
return true;
},
View
11 www-example/localhost.dev/example_dynamic/index.js
@@ -2,10 +2,17 @@ var example_lib = {
init : function(opts){
// opts : request, config, querystring
- return JSON.stringify({'config' : opts['config']});
+
+ //return JSON.stringify({'config' : opts['config']});
+ var $return = {
+ 'content' : JSON.stringify({ 'config' : opts['config'] }),
+ 'status_code' : 200,
+ 'content_type' : 'text/javascript; charset=utf-8'
+ };
+ return $return;
}
};
-exports.init = function(opts){ return example_lib.init(opts); }
+exports.init = function(opts){ return example_lib.init(opts); };

0 comments on commit 3849ad8

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