Add own callback for notFound
This commit is contained in:
parent
7b9fad3090
commit
d79671b0c9
@ -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();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user