diff --git a/src/www/appserver/AppServer.js b/src/www/appserver/AppServer.js index c8f15f3..5950116 100644 --- a/src/www/appserver/AppServer.js +++ b/src/www/appserver/AppServer.js @@ -11,9 +11,9 @@ export default class AppServer { // Why? because SOLID of this class this.webserver = webserver; this.routes = []; + this.notFoundCallback = null; this.onRequest = this.onRequest.bind(this); - this.initWebserver(); this.initRouter(); } @@ -30,12 +30,21 @@ export default class AppServer { this.routes.push( { path: path, - action: () => callback + action: (context) => { + return { + callback: callback, + context: context + }; + } } ); this.initRouter(); } + addNotFound(callback) { + this.notFoundCallback = callback; + } + onRequest(request) { let requestObject = new Request( request.requestId, @@ -55,14 +64,19 @@ export default class AppServer { requestObject.path ).then( // callback is a function - (callback) => { + (callbackContext) => { // run the callback with all information we got for the request and the response - callback(requestObject, responseObject); + requestObject.params = callbackContext.context.params; + callbackContext.callback(requestObject, responseObject); } ).catch( (error) => { - // if there is an error, just send a not found 404 bljad - responseObject.notFound(); + if (!this.notFoundCallback) { + // if there is an error, just send a not found 404 bljad + responseObject.notFound(); + } else { + this.notFoundCallback(); + } } ); } diff --git a/src/www/appserver/Response.js b/src/www/appserver/Response.js index 9c62889..b1a6ac1 100644 --- a/src/www/appserver/Response.js +++ b/src/www/appserver/Response.js @@ -14,6 +14,8 @@ export default class Response { this.status = status; this.body = body; this.headers = headers; + + this.send = this.send.bind(this); } send() { @@ -34,15 +36,15 @@ export default class Response { } notFound(){ - return this.status(404).send(); + this.status(404).send(); } methodNotAllowed() { - return this.status(405).send(); + this.status(405).send(); } ok() { - return this.status(200).send(); + this.status(200).send(); } setHeader(key, value) {