Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed some sync functions to async

  • Loading branch information...
commit b307c860c4b88257f620fe9ac35afda98a7e5971 1 parent 7b81ab0
@pwlin authored
Showing with 39 additions and 17 deletions.
  1. +39 −17 lib/ananas/index.js
View
56 lib/ananas/index.js
@@ -158,8 +158,18 @@ var ananas = {
if (stats) {
if(stats.isDirectory()) {
if(config['directory_listing'] === true && !path.existsSync(path.join(filename , config['directory_index']))) {
- data = self.print_directory_listing({'parent' : filename, 'files' : fs.readdirSync(filename), 'config' : config});
- content_type = 'text/html; charset=utf-8';
+ return fs.readdir(filename, function(err, files){
+ return self.serve({
+ 'request': request,
+ 'response' : response,
+ 'filename' : filename,
+ 'status_code' : status_code,
+ 'stats' : stats,
+ 'config' : config,
+ 'data' : self.print_directory_listing({'parent' : filename, 'files' : files, 'config' : config}),
+ 'content_type' : 'text/html; charset=utf-8'
+ });
+ });
}
else {
filename = path.join(filename, config['directory_index']);
@@ -167,20 +177,32 @@ var ananas = {
}
}
else {
- stats = false;
- try {
- stats = fs.statSync(filename + '.html');
- }
- catch(e) {
- //console.log(e);
- }
- if(stats) {
- filename += '.html';
- }
- else {
- filename = path.join(config['www_root'], '404.html');
- status_code = 404;
- }
+ return fs.stat(filename + '.html', function(err, stats){
+ if (err){
+ return self.serve({
+ 'request': request,
+ 'response' : response,
+ 'filename' : path.join(config['www_root'], '404.html'),
+ 'status_code' : 404,
+ 'stats' : false,
+ 'config' : config,
+ 'data' : data,
+ 'content_type' : content_type
+ });
+ }
+ else {
+ return self.serve({
+ 'request': request,
+ 'response' : response,
+ 'filename' : filename += '.html',
+ 'status_code' : status_code,
+ 'stats' : stats,
+ 'config' : config,
+ 'data' : data,
+ 'content_type' : content_type
+ });
+ }
+ });
}
return self.serve({
'request': request,
@@ -303,7 +325,7 @@ var ananas = {
'Connection' : 'close'
};
var modified = true;
- if (opts['stats'].mtime) {
+ if (opts['stats'] && opts['stats'].mtime) {
var mtime = (new Date(opts['request'].headers['if-modified-since'])).valueOf();
if(mtime >= opts['stats'].mtime.valueOf()){
modified = false;
Please sign in to comment.
Something went wrong with that request. Please try again.