Skip to content

Commit

Permalink
feat: create an initServer metho that initializes the server and sets…
Browse files Browse the repository at this point in the history
… up event handlers
  • Loading branch information
teclone committed Jul 22, 2018
1 parent 9b6269c commit df3dfbe
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/modules/RServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default class {
this.server = http.createServer({
ServerResponse: RServerResponse.getClass(this.staticfileServer)
});

this.initServer();
}

/**
Expand All @@ -58,6 +60,23 @@ export default class {
return this.server.listening;
}

/**
* starts the serve to listen on a given port
*@param {number} port - the port to listen on.
*/
listen(port) {
port = port || 8131;
this.server.listen(port);
console.info('\x1b[32m%s\x1b[0m', 'Server started on port: ' + port);
}

/**
* closes the connection
*/
close() {
this.server.close();
}

/**
* returns the project's entry path
*@param {string} knownPath - the known path for the mean time
Expand Down Expand Up @@ -95,4 +114,31 @@ export default class {

return config;
}

/**
* handle server close event
*/
onClose() {
console.log('connection closed successfully');
}

/**
* handles client error events
*@param {Error} err - client error
*@param {net.Socket} socket - the socket object
*/
onClientError(err, socket) {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
}

/**
* binds all event handlers on the server.
*/
initServer() {
//handle server close event
this.server.on('close', this.onClose)

//handle server client error
.on('clientError', this.onClientError);
}
}

0 comments on commit df3dfbe

Please sign in to comment.