Skip to content

Commit

Permalink
Bug fixes and minor improvements
Browse files Browse the repository at this point in the history
* Removed unnecessary console output;
* A better `_event` function
  • Loading branch information
IPRIT authored May 17, 2020
1 parent 964501e commit abb36fc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions modules/@amperka/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Server.prototype.on = function(types, callback) {
types = [types];
}
for (var t in types) {
console.log(t);
if (!this._events[types[t]]) {
this._events[types[t]] = callback;
}
Expand All @@ -23,22 +22,27 @@ Server.prototype.listen = function(port) {

Server.prototype._onPageRequest = function(req, res) {
var request = url.parse(req.url, true);
this._event(request.pathname, request, res);
this._event(request.pathname, req, res);
};

Server.prototype._event = function(eventName, req, res) {
res.send = function(content, headers) {
if (headers === undefined) {
res.writeHead(200, { 'Content-Type': 'text/html' });
} else {
res.writeHead(200, headers);
}
res.write(content);
};

if (this._events[eventName]) {
res.send = function(content, headers) {
if (headers === undefined) {
res.writeHead(200, { 'Content-Type': 'text/html' });
} else {
res.writeHead(200, headers);
}
res.write(content);
};
this._events[eventName](req, res);
res.end();
} else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('<h1>404 - Not found</h1>');
}

res.end();
};

exports.create = function() {
Expand Down

0 comments on commit abb36fc

Please sign in to comment.