42 lines
829 B
JavaScript
42 lines
829 B
JavaScript
|
'use strict';
|
||
|
|
||
|
/**
|
||
|
* Base Class for wrapping HTTP/HTTPS servers
|
||
|
*/
|
||
|
class Server {
|
||
|
constructor() {}
|
||
|
/**
|
||
|
* Event listener for HTTP(s) server "error" event.
|
||
|
*
|
||
|
* @param {error} error - the error object
|
||
|
* @return {null} - Does not return a value
|
||
|
* @throws {error}
|
||
|
*/
|
||
|
onError(error) {
|
||
|
if (error.syscall !== 'listen') {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
let port = this.server.address().port;
|
||
|
|
||
|
let bind = typeof port === 'string'
|
||
|
? `Pipe ${port}`
|
||
|
: `Port ${port}`;
|
||
|
|
||
|
// handle specific listen errors with friendly messages
|
||
|
switch (error.code) {
|
||
|
case 'EACCES':
|
||
|
logger.error(`${bind} requires elevated privileges`);
|
||
|
process.exit(1);
|
||
|
break;
|
||
|
case 'EADDRINUSE':
|
||
|
logger.error(`${bind} is already in use`);
|
||
|
process.exit(1);
|
||
|
break;
|
||
|
default:
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Server;
|