2017-09-08 04:44:33 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
var app = require('./app')
|
|
|
|
var debug = require('debug')('nodejs-get-started:server')
|
|
|
|
var http = require('http')
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get port from environment and store in Express.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
var port = normalizePort(process.env.PORT || '3000')
|
|
|
|
app.set('port', port)
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HTTP server.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
var server = http.createServer(app)
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen on provided port, on all network interfaces.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
server.listen(port)
|
|
|
|
server.on('error', onError)
|
|
|
|
server.on('listening', onListening)
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
function normalizePort (val) {
|
|
|
|
var port = parseInt(val, 10)
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
2018-01-18 00:11:36 +03:00
|
|
|
return val
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
2018-01-18 00:11:36 +03:00
|
|
|
return port
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
return false
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
function onError (error) {
|
2017-09-08 04:44:33 +03:00
|
|
|
if (error.syscall !== 'listen') {
|
2018-01-18 00:11:36 +03:00
|
|
|
throw error
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var bind = typeof port === 'string'
|
|
|
|
? 'Pipe ' + port
|
2018-01-18 00:11:36 +03:00
|
|
|
: 'Port ' + port
|
2017-09-08 04:44:33 +03:00
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
2018-01-18 00:11:36 +03:00
|
|
|
console.error(bind + ' requires elevated privileges')
|
|
|
|
process.exit(1)
|
|
|
|
break
|
2017-09-08 04:44:33 +03:00
|
|
|
case 'EADDRINUSE':
|
2018-01-18 00:11:36 +03:00
|
|
|
console.error(bind + ' is already in use')
|
|
|
|
process.exit(1)
|
|
|
|
break
|
2017-09-08 04:44:33 +03:00
|
|
|
default:
|
2018-01-18 00:11:36 +03:00
|
|
|
throw error
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:11:36 +03:00
|
|
|
function onListening () {
|
|
|
|
var addr = server.address()
|
2017-09-08 04:44:33 +03:00
|
|
|
var bind = typeof addr === 'string'
|
|
|
|
? 'pipe ' + addr
|
2018-01-18 00:11:36 +03:00
|
|
|
: 'port ' + addr.port
|
|
|
|
debug('Listening on ' + bind)
|
2017-09-08 04:44:33 +03:00
|
|
|
}
|