-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
executable file
·113 lines (100 loc) · 2.67 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
process.title = 'hotcode';
var argv = require('optimist').usage('Usage: $0 -p [num] -h [host.local] -s').argv;
process.addListener('uncaughtException', function (err, stack) {
console.log('Caught exception: '+err+'\n'+err.stack);
console.log('\u0007'); // Terminal bell
});
var express = require('express');
var assetManager = require('connect-assetmanager');
var assetHandler = require('connect-assetmanager-handlers');
var exec = require('child_process').exec;
var port;
if (!argv.p && argv.h) {
port = 80;
} else if (argv.p) {
port = argv.p;
} else {
port = 8080;
}
var host = argv.h ? 'http://'+argv.h.replace(/\/ *$/, '')+':'+port : 'http://127.0.0.1:'+port;
console.log('Running hotcode on: '+host);
console.log('Inject js: '+host+'/static/injected.js');
var app = module.exports = express.createServer();
app.listen(port, null);
// Setup socket.io server
var socketIo = new require('./lib/socket-io-server.js')(app);
// Setup groups for CSS / JS assets
var assetsMiddleware = assetManager({
'js': {
'route': /\/static\/js\/[a-z0-9]+\/.*\.js/
, 'path': __dirname+'/public/js/'
, 'dataType': 'javascript'
, 'files': [
'http://code.jquery.com/jquery-latest.js'
, 'json2.js'
, 'http://127.0.0.1:'+port+'/socket.io/socket.io.js' // special case since the socket.io module serves its own js
, 'jquery.client.js'
]
, 'debug': true
, 'postManipulate': {
'^': [
function insertSocketIoPort(file, path, index, isLast, callback) {
callback(file.replace(/'#socketIoPort#'/, port));
if (argv.s === undefined) {
// Open the browser window then the js is generated.
exec('open '+host, function() {});
}
}
]
}
}
, 'js-inject': {
'route': /\/static(\/[a-z0-9]+)?\/injected.js/
, 'path': __dirname+'/public/js/'
, 'dataType': 'javascript'
, 'files': [
'json2.js'
, 'injected.js'
]
}
, 'css': {
'route': /\/static\/css\/[a-z0-9]+\/.*\.css/
, 'path': __dirname+'/public/css/'
, 'dataType': 'css'
, 'files': [
'reset.css'
, 'client.css'
]
, 'postManipulate': {
'^': [
assetHandler.replaceImageRefToBase64(__dirname+'/public')
]
}
}
});
// Settings
app.configure(function() {
app.set('view engine', 'ejs');
app.set('views', __dirname+'/views');
});
// Middleware
app.configure(function() {
app.use(assetsMiddleware);
app.use(express['static'](__dirname+'/public', {maxAge: 86400000}));
});
// Error handling
app.use(express.errorHandler({
'dumpExceptions': true
, 'showStack': true
}));
// Template helpers
app.dynamicHelpers({
'assetsCacheHashes': function(req, res) {
return assetsMiddleware.cacheHashes;
}
});
// Routing
app.all('*', function(req, res) {
res.render('index');
});