forked from mozilla/node-hubble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (55 loc) · 1.41 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
var express = require( 'express' ),
request = require( 'request' ),
cacheMiddleware = require( './lib/cache-middleware' ),
cache = cacheMiddleware.cache,
util = require( 'util' ),
app = express(),
port = process.env.PORT || 8888,
server;
/**
* Discover the mime type for a given resource, following redirects
*/
function getContentType( url, callback ) {
request({
method: 'HEAD',
url: url,
followAllRedirects: true
},
function( err, res ) {
if ( err ) {
callback( err );
return;
}
callback( null, {
href: res.request.href,
contentType: res.headers[ 'content-type' ]
});
});
}
/**
* http://localhost:8888/url/<url>
*/
app.get( '/url/*', cacheMiddleware.cacheCheck, function( req, res ) {
var url = req.params[ 0 ];
if ( !url ) {
res.jsonp( { error: 'Expected url param, found none.' }, 500 );
return;
}
getContentType( url, function( err, result ) {
if ( err ) {
res.jsonp( { error: 'Unable to determine content type.' }, 500 );
return;
}
cache( url, result );
res.jsonp( result );
});
});
server = app.listen( port, function() {
var addy = server.address();
util.log( 'HTTP Server started on port ' + addy.port );
util.log( 'Press Ctrl+C to stop' );
// If we're running as a child process, let our parent know we're ready.
if ( process.send ) {
process.send( 'Started' );
}
});