Sia-EventUI/server.js

91 строка
1.5 KiB
JavaScript
Исходник Обычный вид История

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