• Add a method of a controller as a response serializer.

    Returns MethodDecorator

    The new decorator function.

    Example

    import { Controller, ControllerBase, GET, IHttpRequest, IHttpResponse, Serializer } from '@egomobile/http-server'

    @Controller()
    export default class MyController extends ControllerBase {
    @GET()
    async index(request: IHttpRequest, response: IHttpResponse) {
    // this will be serialized and send
    // by 'serializeResponse()' method (s. below)
    return {
    success: true,
    data: 'foo'
    }
    }

    @Serializer() // mark that method as default serializer
    // inside that controller
    async serializeResponse(result: any, request: IHttpRequest, response: IHttpResponse) {
    const jsonResponse = Buffer.from(JSON.stringify(result), 'utf8')

    response.writeHead(200, {
    'Content-Length': String(jsonResponse.length),
    'Content-Type': 'application/json; charset=UTF-8'
    });
    response.write(jsonResponse)
    }
    }

Generated using TypeDoc