Add support for view engines, PNPM
This commit is contained in:
@@ -4,7 +4,7 @@ import {HTTPKernelModule} from "./HTTPKernelModule";
|
||||
import {Logging} from "../../service/Logging";
|
||||
import {AppClass} from "../../lifecycle/AppClass";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {http} from "../response/HTTPErrorResponseFactory";
|
||||
import {error} from "../response/ErrorResponseFactory";
|
||||
|
||||
/**
|
||||
* Interface for fluently registering kernel modules into the kernel.
|
||||
@@ -73,7 +73,7 @@ export class HTTPKernel extends AppClass {
|
||||
}
|
||||
} catch (e: any) {
|
||||
this.logging.error(e)
|
||||
await http(HTTPStatus.REQUEST_TIMEOUT).write(request)
|
||||
await error(e).status(HTTPStatus.INTERNAL_SERVER_ERROR).write(request)
|
||||
}
|
||||
|
||||
this.logging.verbose('Finished kernel lifecycle')
|
||||
|
||||
22
src/http/response/ViewResponseFactory.ts
Normal file
22
src/http/response/ViewResponseFactory.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {Container} from "@extollo/di";
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ViewEngine} from "../../views/ViewEngine";
|
||||
|
||||
export function view(name: string, data?: {[key: string]: any}): ViewResponseFactory {
|
||||
return new ViewResponseFactory(name, data)
|
||||
}
|
||||
|
||||
export class ViewResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly viewName: string,
|
||||
public readonly data?: {[key: string]: any}
|
||||
) { super() }
|
||||
|
||||
public async write(request: Request) {
|
||||
const viewEngine = <ViewEngine> Container.getContainer().make(ViewEngine)
|
||||
request.response.body = await viewEngine.renderByName(this.viewName, this.data || {})
|
||||
request.response.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
return request
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user