|
1 | 1 | #!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | +/* eslint-disable complexity */ |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Module dependencies. |
5 | 7 | */ |
6 | 8 |
|
7 | | -const app = require('../app'); |
8 | | -const debug = require('debug')('deployer:server'); |
9 | | -const http = require('http'); |
| 9 | +const app = require('../app') |
| 10 | +const debug = require('debug')('deployer:server') |
| 11 | +const http = require('http') |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * Get port from environment and store in Express. |
13 | 15 | */ |
14 | 16 |
|
15 | | -const port = normalizePort(process.env.PORT || '3000'); |
16 | | -app.set('port', port); |
| 17 | +const port = normalizePort(process.env.PORT || '3000') |
| 18 | +app.set('port', port) |
17 | 19 |
|
18 | 20 | /** |
19 | 21 | * Create HTTP server. |
20 | 22 | */ |
21 | 23 |
|
22 | | -const server = http.createServer(app); |
| 24 | +const server = http.createServer(app) |
23 | 25 |
|
24 | 26 | /** |
25 | 27 | * Listen on provided port, on all network interfaces. |
26 | 28 | */ |
27 | 29 |
|
28 | | -server.listen(port); |
29 | | -server.on('error', onError); |
30 | | -server.on('listening', onListening); |
| 30 | +server.listen(port) |
| 31 | +server.on('error', onError) |
| 32 | +server.on('listening', onListening) |
31 | 33 |
|
32 | 34 | /** |
33 | 35 | * Normalize a port into a number, string, or false. |
34 | 36 | */ |
35 | 37 |
|
36 | 38 | function normalizePort(val) { |
37 | | - const port = parseInt(val, 10); |
| 39 | + const port = parseInt(val, 10) |
38 | 40 |
|
39 | | - if (isNaN(port)) { |
| 41 | + if (isNaN(port)) { |
40 | 42 | // named pipe |
41 | | - return val; |
42 | | - } |
| 43 | + return val |
| 44 | + } |
43 | 45 |
|
44 | | - if (port >= 0) { |
| 46 | + if (port >= 0) { |
45 | 47 | // port number |
46 | | - return port; |
47 | | - } |
| 48 | + return port |
| 49 | + } |
48 | 50 |
|
49 | | - return false; |
| 51 | + return false |
50 | 52 | } |
51 | 53 |
|
52 | 54 | /** |
53 | 55 | * Event listener for HTTP server "error" event. |
54 | 56 | */ |
55 | 57 |
|
56 | 58 | function onError(error) { |
57 | | - if (error.syscall !== 'listen') { |
58 | | - throw error; |
59 | | - } |
| 59 | + if (error.syscall !== 'listen') { |
| 60 | + throw error |
| 61 | + } |
60 | 62 |
|
61 | | - const bind = typeof port === 'string' |
62 | | - ? 'Pipe ' + port |
63 | | - : 'Port ' + port; |
| 63 | + const bind = typeof port === 'string' |
| 64 | + ? 'Pipe ' + port |
| 65 | + : 'Port ' + port |
64 | 66 |
|
65 | | - // handle specific listen errors with friendly messages |
66 | | - switch (error.code) { |
| 67 | + // handle specific listen errors with friendly messages |
| 68 | + switch (error.code) { |
67 | 69 | case 'EACCES': |
68 | | - console.error(bind + ' requires elevated privileges'); |
69 | | - process.exit(1); |
70 | | - break; |
| 70 | + console.error(bind + ' requires elevated privileges') |
| 71 | + process.exit(1) |
| 72 | + break |
71 | 73 | case 'EADDRINUSE': |
72 | | - console.error(bind + ' is already in use'); |
73 | | - process.exit(1); |
74 | | - break; |
| 74 | + console.error(bind + ' is already in use') |
| 75 | + process.exit(1) |
| 76 | + break |
75 | 77 | default: |
76 | | - throw error; |
77 | | - } |
| 78 | + throw error |
| 79 | + } |
78 | 80 | } |
79 | 81 |
|
80 | 82 | /** |
81 | 83 | * Event listener for HTTP server "listening" event. |
82 | 84 | */ |
83 | 85 |
|
84 | 86 | function onListening() { |
85 | | - const addr = server.address(); |
86 | | - const bind = typeof addr === 'string' |
87 | | - ? 'pipe ' + addr |
88 | | - : 'port ' + addr.port; |
89 | | - debug('Listening on ' + bind); |
| 87 | + const addr = server.address() |
| 88 | + const bind = typeof addr === 'string' |
| 89 | + ? 'pipe ' + addr |
| 90 | + : 'port ' + addr.port |
| 91 | + debug('Listening on ' + bind) |
90 | 92 | } |
0 commit comments