forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-server.js
52 lines (46 loc) · 1.46 KB
/
local-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var http = require("http"),
url = require("url"),
fs = require("fs"),
tachyon = require( './index' ),
args = process.argv.slice(2),
port = Number( args[0] ) ? args[0] : 8080,
debug = args.indexOf( '--debug' ) > -1;
http.createServer( function( request, response ) {
var params = url.parse( request.url, true );
if ( debug ) {
console.log( Date(), request.url );
}
try {
var imageData = fs.readFileSync( decodeURI( params.pathname.substr(1) ) );
} catch ( err ) {
response.writeHead( err.statusCode ? err.statusCode : 500 );
response.write( err.message );
return response.end();
}
params.query.key = decodeURI( params.pathname.substr(1) );
return tachyon.resizeBuffer( imageData, params.query, function( err, data, info ) {
if ( err ) {
if ( err.message === 'fallback-to-original' ) {
response.writeHead( 200, {
'Content-Type': 'image/gif',
'Content-Length': Buffer.byteLength( imageData ),
} );
response.write( imageData );
return response.end();
}
if ( debug ) {
console.error( Date(), err );
}
response.writeHead( err.statusCode ? err.statusCode : 500 );
response.write( err.message );
return response.end();
}
response.writeHead( 200, {
'Content-Type': 'image/' + info.format,
'Content-Length': info.size,
} );
response.write( data );
return response.end();
} );
} ).listen( parseInt( port, 10 ) );
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" )