# Response

The Response object in FhyTS is a wrapper for **http.ServerResponse**, which simplifies sending HTTP responses, including setting statuses, headers, and response formats like JSON and HTML.

## API Reference

Here are the main properties and methods available:

| Property / Method | Type | Description |
| ---------------------- | --------------------------------------- | ---------------------------------------------------------------------- |
| `headersSent` | `boolean` | Indicates whether headers have been sent to the client. |
| `status(code)` | `(code: number) => this` | Sets the HTTP response status (e.g., 200, 404, 500). |
| `setHeader(name, val)` | `(name: string, value: string) => this` | Manually sets HTTP headers (if not already sent). |
| `json(data)` | `(data: any) => void` | Sends a JSON response with the header `Content-Type: application/json`. |
| `html(html)` | `(html: string) => void` | Sends an HTML response with the header `Content-Type: text/html`. |
| `send(data)` | `(data: string \| Buffer) => void` | Sends raw data to the client (string or buffer). |

## Usage Examples

#### Sending JSON

```javascript
export const api = async (req: Request, res: Response) => {
  res.status(200).json({ message: 'Success', timestamp: Date.now() });
};
```

#### Sending HTML

```javascript
export const page = async (req: Request, res: Response) => {
  res.status(200).html('<h1>Welcome to FhyTS</h1>');
};
```

#### Manually Assign Headers

```javascript
export const download = async (req: Request, res: Response) => {
  res
    .setHeader('Content-Disposition', 'attachment; filename="data.txt"')
    .setHeader('Content-Type', 'text/plain')
    .send('This is the file content.');
};
```

## Automatic Integration

The **Response** object is automatically provided in the controller and middleware context via the **res** parameter. There's no need to manually create an instance.

```javascript
export const handler = async (req: Request, res: Response) => {
  res.status(201).json({ created: true });
};
```

## Best Practices

* Make sure to only call one response method (**json**, **html**, or **send**) per request.
* Use **headersSent** to prevent header modification after the response is sent.
* Always set **Content-Type** explicitly when using send.

## Next

Now that you understand how to use Response, you can move on to the [DI Container](#di-container) section to learn how to efficiently manage dependencies and service injection in your application.