index.ts 1.69 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
#!/usr/bin/env node

/**
 * Module dependencies.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
7
// import {app} from './app';
Rosanny Sihombing's avatar
Rosanny Sihombing committed
8
import http from 'http'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
9
10
const app = require('./app')
const debug = require('debug')('MLAB-Account:server')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
11
12
13
14
15
16
17
18

const env = process.env.NODE_ENV ?? 'testing'
const config = require('./config/config')[env]

/**
 * Get port from environment and store in Express.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
20
const port = normalizePort(process.env.PORT || config.app.port)
app.set('port', port)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
21
22
23
24
25

/**
 * Create HTTP server.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
26
const server = http.createServer(app)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
27
28
29
30
31

/**
 * Listen on provided port, on all network interfaces.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
32
33
34
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
35
36
37
38
39

/**
 * Normalize a port into a number, string, or false.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
40
41
function normalizePort (val: any) {
  const port = parseInt(val, 10)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
42
43
44

  if (isNaN(port)) {
    // named pipe
Rosanny Sihombing's avatar
Rosanny Sihombing committed
45
    return val
Rosanny Sihombing's avatar
Rosanny Sihombing committed
46
47
48
49
  }

  if (port >= 0) {
    // port number
Rosanny Sihombing's avatar
Rosanny Sihombing committed
50
    return port
Rosanny Sihombing's avatar
Rosanny Sihombing committed
51
52
  }

Rosanny Sihombing's avatar
Rosanny Sihombing committed
53
  return false
Rosanny Sihombing's avatar
Rosanny Sihombing committed
54
55
56
57
58
59
}

/**
 * Event listener for HTTP server "error" event.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
60
function onError (error: any) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
61
  if (error.syscall !== 'listen') {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
62
    throw error
Rosanny Sihombing's avatar
Rosanny Sihombing committed
63
64
  }

Rosanny Sihombing's avatar
Rosanny Sihombing committed
65
  const bind = typeof port === 'string'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
66
    ? 'Pipe ' + port
Rosanny Sihombing's avatar
Rosanny Sihombing committed
67
    : 'Port ' + port
Rosanny Sihombing's avatar
Rosanny Sihombing committed
68
69
70
71

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
Rosanny Sihombing's avatar
Rosanny Sihombing committed
72
73
74
      console.error(bind + ' requires elevated privileges')
      process.exit(1)
      break
Rosanny Sihombing's avatar
Rosanny Sihombing committed
75
    case 'EADDRINUSE':
Rosanny Sihombing's avatar
Rosanny Sihombing committed
76
77
78
      console.error(bind + ' is already in use')
      process.exit(1)
      break
Rosanny Sihombing's avatar
Rosanny Sihombing committed
79
    default:
Rosanny Sihombing's avatar
Rosanny Sihombing committed
80
      throw error
Rosanny Sihombing's avatar
Rosanny Sihombing committed
81
82
83
84
85
86
87
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

Rosanny Sihombing's avatar
Rosanny Sihombing committed
88
89
90
function onListening () {
  const addr = server.address()
  const bind = typeof addr === 'string'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
91
    ? 'pipe ' + addr
Rosanny Sihombing's avatar
Rosanny Sihombing committed
92
93
    : 'port ' + addr!.port
  debug('Listening on ' + bind)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
94
}