Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
(failed) http node.js server
var http = require( 'http' );
var url = require( 'url' );
var fs = require( 'fs' );
var path = require( 'path' );
var gzip = require( './lib/gzip' );
var VERBOSE = false;
var cache = {}, count = 0;
http.createServer( function ( request, response ) {
VERBOSE && console.log( '<--', request.headers.host, request.url );
// CACHE
var uri = sites( request.headers.host, url.parse( request.url ).pathname );
if ( cache[ uri ] ) {
var cached = cache[ uri ];
if ( cached.exists ) {
if ( request.headers['if-modified-since'] ) {
var mtime = new Date( request.headers['if-modified-since'] );
if ( mtime >= cached.mtime ) {
VERBOSE && console.log( '-->', 'CACHED', 304 );
response.writeHead( 304 );
response.end();
return;
}
}
fs.readFile( cached.filename, 'binary', function ( error, file ) {
VERBOSE && console.log( '-->', 'CACHED', 200, cached.size );
response.writeHead( 200, {
'Content-Type': cached.mtype,
'Content-Encoding': cached.gzip ? 'gzip' : 'identity',
'Content-Length': cached.size,
'Date': new Date().toString(),
'Last-Modified': cached.mtime,
'Keep-Alive': 'timeout=15, max=100'
} );
response.write( file, 'binary' );
response.end();
} );
} else {
VERBOSE && console.log( '-->', 'CACHED', 404 );
response.writeHead( 404 );
response.end();
}
return;
}
filename = uri;
path.exists( filename, function ( exists ) {
if ( !exists ) {
cache[ uri ] = { 'exists': false };
VERBOSE && console.log( '-->', 404 );
response.writeHead( 404 );
response.end();
return;
}
fs.stat( filename, function ( error, stats ) {
if ( stats.isDirectory() ) filename += '/index.html';
fs.readFile( filename, 'binary', function ( error, file ) {
if ( error ) {
cache[ uri ] = { 'exists': false };
VERBOSE && console.log( '-->', 404 );
response.writeHead( 404 );
response.end();
return;
}
var ext = path.extname( filename ).toLowerCase();
var mtype = mimetype( ext );
var size = stats.size;
var mtime = stats.mtime;
if ( compressible( ext ) ) {
gzip( file, function ( error, data ) {
filename = '../tmp/' + ( count ++ );
fs.writeFile( filename, data, function ( error ) {
if ( error ) throw error;
fs.stat( filename, function ( error, stats2 ) {
cache[ uri ] = {
'exists': true,
'gzip': true,
'filename': filename,
'mtype': mtype,
'size': stats2.size,
'mtime': mtime
};
VERBOSE && console.log( '-->', 200, stats.size, stats2.size );
response.writeHead( 200, {
'Content-Type': mtype,
'Content-Encoding': 'gzip',
'Content-Length': stats2.size,
'Date': new Date().toString(),
'Last-Modified': mtime,
'Keep-Alive': 'timeout=15, max=100'
} );
response.write( data, 'binary' );
response.end();
} );
} );
} );
} else {
cache[ uri ] = {
'exists': true,
'gzip': false,
'filename': filename,
'mtype': mtype,
'size': size,
'mtime': mtime
};
VERBOSE && console.log( '-->', 200, stats.size );
response.writeHead( 200, {
'Content-Type': mtype,
'Content-Length': size,
'Date': new Date().toString(),
'Last-Modified': mtime,
'Keep-Alive': 'timeout=15, max=100'
} );
response.write( file, 'binary' );
response.end();
}
} );
} );
} );
} ).listen( 80 );
function sites( domain, uri ) {
switch ( domain ) {
case 'doobslice:1337':
case 'mrdoob.com':
case 'www.mrdoob.com':
if ( uri.substr( -1 ) == '/' ) uri += 'index.html';
if ( /^\/(\d+)\//.test( uri ) ) uri = '/index.html'; // showcase
return path.join( '../www/mrdoob.com', uri );
case '60fps.com':
case 'www.60fps.com':
if ( uri.substr( -1 ) == '/' ) uri += 'index.html';
return path.join( '../www/60fps.com', uri );
case 'xplsv.tv':
case 'www.xplsv.tv':
if ( uri.substr( -1 ) == '/' ) uri += 'index.html';
return path.join( '../www/xplsv.tv', uri );
default:
return '';
}
}
function mimetype( extension ) {
switch ( extension ) {
case '.ico': return 'image/x-icon';
case '.html': case '.htm': return 'text/html';
case '.css': return 'text/css';
case '.js': return 'text/javascript';
case '.json': return 'text/json';
case '.png': return 'image/png';
case '.jpeg': case '.jpg': case '.jpe': return 'image/jpeg';
case '.oga': case '.ogg': case '.spx': return 'audio/ogg';
case '.mpga': case '.mp2': case '.mp2a': case '.mp3': case '.m2a': case '.m3a': return 'audio/mpeg';
default: return 'text/plain';
}
}
function compressible( extension ) {
switch ( extension ) {
case '.html': case '.htm': case '.css': case '.js': case '.json': return true;
default: return false;
}
}
VERBOSE && console.log( 'Server running.' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.