|
@@ -13,10 +13,10 @@
|
|
*/
|
|
*/
|
|
|
|
|
|
var port = 8000,
|
|
var port = 8000,
|
|
- http = require('http'),
|
|
|
|
- urlParser = require('url'),
|
|
|
|
- fs = require('fs'),
|
|
|
|
- path = require('path'),
|
|
|
|
|
|
+ http = require( 'http' ),
|
|
|
|
+ urlParser = require( 'url' ),
|
|
|
|
+ fs = require( 'fs' ),
|
|
|
|
+ path = require( 'path' ),
|
|
currentDir = process.cwd(),
|
|
currentDir = process.cwd(),
|
|
mimeTypes = {
|
|
mimeTypes = {
|
|
"html": "text/html",
|
|
"html": "text/html",
|
|
@@ -32,88 +32,103 @@ var port = 8000,
|
|
"bin": "application/octet-stream"
|
|
"bin": "application/octet-stream"
|
|
};
|
|
};
|
|
|
|
|
|
-port = process.argv[2] ? parseInt(process.argv[2], 0) : port;
|
|
|
|
|
|
+port = process.argv[ 2 ] ? parseInt( process.argv[ 2 ], 0 ) : port;
|
|
|
|
|
|
-function handleRequest(request, response) {
|
|
|
|
|
|
+function handleRequest( request, response ) {
|
|
|
|
|
|
- var urlObject = urlParser.parse(request.url, true);
|
|
|
|
- var pathname = decodeURIComponent(urlObject.pathname);
|
|
|
|
|
|
+ var urlObject = urlParser.parse( request.url, true );
|
|
|
|
+ var pathname = decodeURIComponent( urlObject.pathname );
|
|
|
|
|
|
- console.log('[' + (new Date()).toUTCString() + '] ' + '"' + request.method + ' ' + pathname + '"');
|
|
|
|
|
|
+ console.log( '[' + ( new Date() ).toUTCString() + '] ' + '"' + request.method + ' ' + pathname + '"' );
|
|
|
|
|
|
- var filePath = path.join(currentDir, pathname);
|
|
|
|
|
|
+ var filePath = path.join( currentDir, pathname );
|
|
|
|
|
|
- fs.stat(filePath, function(err, stats) {
|
|
|
|
|
|
+ fs.stat( filePath, function ( err, stats ) {
|
|
|
|
|
|
- if (err) {
|
|
|
|
- response.writeHead(404, {});
|
|
|
|
- response.end('File not found!');
|
|
|
|
|
|
+ if ( err ) {
|
|
|
|
+
|
|
|
|
+ response.writeHead( 404, {} );
|
|
|
|
+ response.end( 'File not found!' );
|
|
return;
|
|
return;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
- if (stats.isFile()) {
|
|
|
|
|
|
+ if ( stats.isFile() ) {
|
|
|
|
|
|
- fs.readFile(filePath, function(err, data) {
|
|
|
|
|
|
+ fs.readFile( filePath, function ( err, data ) {
|
|
|
|
|
|
- if (err) {
|
|
|
|
- response.writeHead(404, {});
|
|
|
|
- response.end('Opps. Resource not found');
|
|
|
|
|
|
+ if ( err ) {
|
|
|
|
+
|
|
|
|
+ response.writeHead( 404, {} );
|
|
|
|
+ response.end( 'Opps. Resource not found' );
|
|
return;
|
|
return;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
- var fileType = filePath.split('.').pop().toLowerCase();
|
|
|
|
|
|
+ var fileType = filePath.split( '.' ).pop().toLowerCase();
|
|
|
|
|
|
- response.writeHead(200, {
|
|
|
|
- "Content-Type": mimeTypes[fileType] || mimeTypes['bin']
|
|
|
|
|
|
+ response.writeHead( 200, {
|
|
|
|
+ "Content-Type": mimeTypes[ fileType ] || mimeTypes[ 'bin' ]
|
|
} );
|
|
} );
|
|
|
|
|
|
- response.write(data);
|
|
|
|
|
|
+ response.write( data );
|
|
response.end();
|
|
response.end();
|
|
|
|
|
|
- });
|
|
|
|
|
|
+ } );
|
|
|
|
|
|
- } else if (stats.isDirectory()) {
|
|
|
|
|
|
+ } else if ( stats.isDirectory() ) {
|
|
|
|
|
|
- fs.readdir(filePath, function(error, files) {
|
|
|
|
|
|
+ fs.readdir( filePath, function ( error, files ) {
|
|
|
|
|
|
- if (error) {
|
|
|
|
- response.writeHead(500, {});
|
|
|
|
|
|
+ if ( error ) {
|
|
|
|
+
|
|
|
|
+ response.writeHead( 500, {} );
|
|
response.end();
|
|
response.end();
|
|
return;
|
|
return;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
var l = pathname.length;
|
|
var l = pathname.length;
|
|
- if (pathname.substring(l-1)!='/') pathname += '/';
|
|
|
|
|
|
+ if ( pathname.substring( l - 1 ) != '/' ) pathname += '/';
|
|
|
|
|
|
- response.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
- response.write('<!DOCTYPE html>\n<html><head><meta charset="UTF-8"><title>' + filePath + '</title></head><body>');
|
|
|
|
- response.write('<h1>' + filePath + '</h1>');
|
|
|
|
- response.write('<ul style="list-style:none;font-family:courier new;">');
|
|
|
|
- files.unshift('.', '..');
|
|
|
|
- files.forEach(function(item) {
|
|
|
|
|
|
+ response.writeHead( 200, { 'Content-Type': 'text/html' } );
|
|
|
|
+ response.write( '<!DOCTYPE html>\n<html><head><meta charset="UTF-8"><title>' + filePath + '</title></head><body>' );
|
|
|
|
+ response.write( '<h1>' + filePath + '</h1>' );
|
|
|
|
+ response.write( '<ul style="list-style:none;font-family:courier new;">' );
|
|
|
|
+ files.unshift( '.', '..' );
|
|
|
|
+ files.forEach( function ( item ) {
|
|
|
|
|
|
var urlpath = pathname + item,
|
|
var urlpath = pathname + item,
|
|
- itemStats = fs.statSync(currentDir + urlpath);
|
|
|
|
|
|
+ itemStats = fs.statSync( currentDir + urlpath );
|
|
|
|
+
|
|
|
|
+ if ( itemStats.isDirectory() ) {
|
|
|
|
|
|
- if (itemStats.isDirectory()) {
|
|
|
|
urlpath += '/';
|
|
urlpath += '/';
|
|
item += '/';
|
|
item += '/';
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
- response.write('<li><a href="'+ urlpath + '">' + item + '</a></li>');
|
|
|
|
- });
|
|
|
|
|
|
+ response.write( '<li><a href="' + urlpath + '">' + item + '</a></li>' );
|
|
|
|
+
|
|
|
|
+ } );
|
|
|
|
+
|
|
|
|
+ response.end( '</ul></body></html>' );
|
|
|
|
+
|
|
|
|
+ } );
|
|
|
|
|
|
- response.end('</ul></body></html>');
|
|
|
|
- });
|
|
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+
|
|
|
|
+ } );
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-http.createServer(handleRequest).listen(port);
|
|
|
|
|
|
+http.createServer( handleRequest ).listen( port );
|
|
|
|
+
|
|
|
|
+require( 'dns' ).lookup( require( 'os' ).hostname(), function ( err, addr, fam ) {
|
|
|
|
+
|
|
|
|
+ console.log( 'Running at http://' + addr + ( ( port === 80 ) ? '' : ':' ) + port + '/' );
|
|
|
|
|
|
-require('dns').lookup(require('os').hostname(), function (err, addr, fam) {
|
|
|
|
- console.log('Running at http://' + addr + ((port === 80) ? '' : ':') + port + '/');
|
|
|
|
-});
|
|
|
|
|
|
+} );
|
|
|
|
|
|
-console.log('Three.js server has started...');
|
|
|
|
-console.log('Base directory at ' + currentDir);
|
|
|
|
|
|
+console.log( 'Three.js server has started...' );
|
|
|
|
+console.log( 'Base directory at ' + currentDir );
|