UNPKG

1.44 kBJavaScriptView Raw
1// Runner
2// ------
3//
4// Runs the Express app. Wraps it in supervisor if need be.
5//
6var Runner = module.exports = function(app, port, flags) {
7 var time = { start: +new Date() };
8
9 port = port || 4567;
10
11 // Quick start mode
12 if (flags === 'Q') {
13 app.load();
14 start();
15 }
16
17 // Supervisor mode
18 else if (app.get('env') === 'development') {
19 app.log.info('Auto-restarting on changes');
20 printHeader();
21 runSupervisor();
22 }
23
24 else {
25 app.on('load:before', function() { printHeader(); });
26 app.load();
27 start();
28 }
29
30 // Helpers
31 // -------
32
33 function printHeader() {
34 app.log.info("Running %s mode at http://0.0.0.0:"+port, app.get('env'));
35 }
36
37 // Invokes supervisor to load the runner bin of the application.
38 function runSupervisor() {
39 var supervisor = require('supervisor');
40 supervisor.run([
41 '--quiet',
42 '--extensions', 'node|js|coffee',
43 '--', process.argv[1], 'server', port, 'Q']);
44 }
45
46 function start() {
47 var elapsed = +new Date() - time.start;
48 app.log.info('Ready [' + elapsed + 'ms]');
49 catchExceptions();
50 app.listen(port);
51 }
52
53 /**
54 * Print unhandled exceptions and terminate
55 */
56
57 function catchExceptions() {
58 if (app.get('env') === 'development') {
59 process.on('exit', function() {
60 app.log.debug('Restarting...');
61 });
62 }
63 }
64
65 function timestamp() {
66 return (new Date()).toString().split(' ')[4];
67 }
68};