# PlatformResponse Class platform

Module
import { PlatformResponse } from "@tsed/common"
Source/packages/common/src/platform/services/PlatformResponse.ts

# Overview

class PlatformResponse {
    raw: Res;
    constructor(raw: Res);
    /**
     * Get the current statusCode
     */
    get statusCode(): number;
    /**
     * An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.
     *
     * This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.
     */
    get locals(): Record<string, any>;
    /**
     * Create a new instance of PlatformResponse
     * @param injector
     * @param res
     */
    static create(injector: InjectorService, res: Res): PlatformResponse;
    /**
     * Sets the HTTP status for the response.
     *
     * @param status
     */
    status(status: number): this;
    /**
     * Set header `field` to `val`, or pass
     * an object of header fields.
     *
     * Examples:
     * ```typescript
     * response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
     * ```
     *
     * Aliased as `res.header()`.
     */
    setHeaders(headers: {
        [key: string]: any;
    }): this;
    /**
     * Set `Content-Type` response header with `type` through `mime.lookup()`
     * when it does not contain "/", or set the Content-Type to `type` otherwise.
     *
     * Examples:
     *
     *     res.type('.html');
     *     res.type('html');
     *     res.type('json');
     *     res.type('application/json');
     *     res.type('png');
     */
    contentType(contentType: string): this;
    /**
     * Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
     * If not specified, status defaults to `302 Found`.
     *
     * @param status
     * @param url
     */
    redirect(status: number, url: string): this;
    /**
     * Sets the response Location HTTP header to the specified path parameter.
     *
     * @param location
     */
    location(location: string): this;
    /**
     * Stream the given data.
     *
     * @param data
     */
    stream(data: ReadableStream | any): this;
    /**
     * Renders a view and sends the rendered HTML string to the client.
     *
     * @param path
     * @param options
     */
    render(path: string, options?: any): Promise<string>;
    /**
     * Send any data to your consumer.
     *
     * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
     * It choose the better way to send the data.
     *
     * @param data
     */
    body(data: any): this;
    /**
     * Add a listener to handler the end of the request/response.
     * @param cb
     */
    onEnd(cb: Function): void;
    destroy(): void;
}

# Description

Platform Response abstraction layer.

# Members

raw: Res;

get statusCode(): number;

Get the current statusCode


get locals(): Record<string, any>;

An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.

This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.


static create(injector: InjectorService, res: Res): PlatformResponse;

Create a new instance of PlatformResponse


status(status: number): this;

Sets the HTTP status for the response.


setHeaders(headers: {
     [key: string]: any;
 }): this;

Set header field to val, or pass an object of header fields.

Examples:

response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
1

Aliased as res.header().


contentType(contentType: string): this;

Set Content-Type response header with type through mime.lookup() when it does not contain "/", or set the Content-Type to type otherwise.

Examples:

res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');

redirect(status: number, url: string): this;

Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code. If not specified, status defaults to 302 Found.


location(location: string): this;

Sets the response Location HTTP header to the specified path parameter.


stream(data: ReadableStream | any): this;

Stream the given data.


render(path: string, options?: any): Promise<string>;

Renders a view and sends the rendered HTML string to the client.


body(data: any): this;

Send any data to your consumer.

This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data. It choose the better way to send the data.


onEnd(cb: Function): void;

Add a listener to handler the end of the request/response.


destroy(): void;