Skip to content

Commit 94d846a

Browse files
committed
Merge branch 'master' of github.com:requirejs/example-jquery-shim
2 parents 2870380 + 2f68123 commit 94d846a

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

tools/server.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,36 @@ 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);
2021

2122
fs.exists(filename, function (exists) {
22-
if (!exists) {
23-
response.writeHead(404, {'Content-Type': 'text/plain'});
24-
response.write('404 Not Found\n');
25-
response.end();
26-
return;
27-
}
23+
if (!exists) {
24+
response.writeHead(404, {'Content-Type': 'text/plain'});
25+
response.write('404 Not Found\n');
26+
response.end();
27+
return;
28+
}
2829

29-
var type = filename.split('.');
30-
type = type[type.length - 1];
30+
if(!fs.lstatSync(filename).isDirectory()) {
31+
var type = filename.split('.');
32+
type = type[type.length - 1];
3133

32-
response.writeHead(200, { 'Content-Type': types[type] + '; charset=utf-8' });
33-
fs.createReadStream(filename).pipe(response);
34-
});
34+
response.writeHead(200, { 'Content-Type': types[type] + '; charset=utf-8' });
35+
fs.createReadStream(filename).pipe(response);
36+
} else {
37+
/**
38+
* if users visit the site such as http://localhost:8888
39+
* then lead them to http://localhost:8888/www/app.html
40+
*/
41+
response.writeHead(301, {'Location': site + '/www/app.html' });
42+
response.end();
43+
}
44+
});
3545
}).listen(parseInt(port, 10));
3646

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

0 commit comments

Comments
 (0)