Skip to content

Commit 0486fe0

Browse files
committed
Checking for a directory before we knew if the path exists caused the
server to crash, when the browser look for a favicon. So, do the exists check first, then see if its a file or directory. Also, lets just 301 to the right page, its easier.
1 parent 8d24eaa commit 0486fe0

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

tools/server.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,42 @@ var http = require('http'),
1212
types = {
1313
'html': 'text/html',
1414
'js': 'application/javascript'
15-
};
15+
},
16+
site = 'http://localhost:' + port;
1617

1718
http.createServer(function (request, response) {
1819
var uri = url.parse(request.url).pathname,
1920
filename = path.join(__dirname, '..', uri);
21+
/*
22+
if(/favicon.ico$/.test(uri)) {
23+
response.writeHead(204, {});
24+
return response.end();
25+
}
26+
*/
2027

21-
if(!fs.lstatSync(filename).isDirectory()) {
22-
fs.exists(filename, function (exists) {
23-
if (!exists) {
24-
response.writeHead(404, {'Content-Type': 'text/plain'});
25-
response.write('404 Not Found\n');
26-
response.end();
27-
return;
28-
}
28+
fs.exists(filename, function (exists) {
29+
if (!exists) {
30+
response.writeHead(404, {'Content-Type': 'text/plain'});
31+
response.write('404 Not Found\n');
32+
response.end();
33+
return;
34+
}
2935

36+
if(!fs.lstatSync(filename).isDirectory()) {
3037
var type = filename.split('.');
3138
type = type[type.length - 1];
3239

3340
response.writeHead(200, { 'Content-Type': types[type] + '; charset=utf-8' });
3441
fs.createReadStream(filename).pipe(response);
35-
});
36-
} else {
42+
} else {
3743
/**
3844
* if users visit the site such as http://localhost:8888
3945
* then lead them to http://localhost:8888/www/app.html
40-
*
4146
*/
42-
response.writeHead(200, {'Content-Type': 'text/html'});
43-
response.write('Please visit <a href="www/app.html">here</a>\n');
47+
response.writeHead(301, {'Location': site + '/www/app.html' });
4448
response.end();
45-
}
49+
}
50+
});
4651
}).listen(parseInt(port, 10));
4752

48-
console.log('Static file server running at\n => http://localhost:' + port + '/\nCTRL + C to shutdown');
53+
console.log('Static file server running at\n => ' + site + '/\nCTRL + C to shutdown');

0 commit comments

Comments
 (0)