# OvernightJS Setup

Your server now uses **OvernightJS**, a lightweight Express framework with powerful decorators for building scalable TypeScript servers.

## Key Features

✅ **Decorators** - Clean syntax using `@Controller`, `@Get`, `@Post`, `@Put`, `@Delete`
✅ **Built on Express** - All Express middleware still works
✅ **TypeScript** - Full type safety with decorators
✅ **Automatic Routing** - Routes defined via decorators
✅ **Scalable** - Perfect for large applications

## How It Works

### Controllers with Decorators

Instead of manual routing, use decorators:

```typescript
import { Controller, Get, Post, Put, Delete } from '@overnightjs/core';
import { Request, Response } from 'express';

@Controller('api/users')
export class UserController {
  @Get()
  async getAllUsers(req: Request, res: Response): Promise<void> {
    res.json({ success: true, data: [] });
  }

  @Get(':id')
  async getUser(req: Request, res: Response): Promise<void> {
    const { id } = req.params;
    res.json({ success: true, data: { id } });
  }

  @Post()
  async createUser(req: Request, res: Response): Promise<void> {
    res.status(201).json({ success: true, data: req.body });
  }

  @Put(':id')
  async updateUser(req: Request, res: Response): Promise<void> {
    const { id } = req.params;
    res.json({ success: true, data: { id, ...req.body } });
  }

  @Delete(':id')
  async deleteUser(req: Request, res: Response): Promise<void> {
    res.json({ success: true, message: 'Deleted' });
  }
}
```

This creates routes automatically:
- `GET /api/users`
- `GET /api/users/:id`
- `POST /api/users`
- `PUT /api/users/:id`
- `DELETE /api/users/:id`

### Register Controllers

In `index.ts`, register your controllers:

```typescript
import { Server } from '@overnightjs/core';

class AppServer extends Server {
  constructor() {
    super();
    this.setup();
  }

  private setup(): void {
    const controllers = [
      new UserController(),
      new ProductController(),
      new OrderController()
      // Add more controllers as needed
    ];
    
    this.addControllers(controllers);
  }
}

const appServer = new AppServer();
appServer.start();
```

## Adding New Routes

1. Create a new Controller with `@Controller` decorator
2. Define methods with `@Get`, `@Post`, `@Put`, `@Delete` decorators
3. Add the controller to the `controllers` array in `index.ts`
4. Routes are automatically registered!

## Route Decorators

| Decorator | HTTP Method | Example |
|-----------|-------------|---------|
| `@Get()` | GET | `@Get()` or `@Get('path')` |
| `@Post()` | POST | `@Post()` |
| `@Put()` | PUT | `@Put(':id')` |
| `@Delete()` | DELETE | `@Delete(':id')` |
| `@Patch()` | PATCH | `@Patch(':id')` |

## Current Controllers

✅ **PatientController** (`@Controller('api/patients')`)
✅ **DoctorController** (`@Controller('api/doctors')`)
✅ **AppointmentController** (`@Controller('api/appointments')`)

## Middleware Support

Middleware still works as normal:

```typescript
app.use(express.json());
app.use(cors());
app.use(authentication); // Your custom middleware
```

## Error Handling

```typescript
// Global error handler still works
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});
```

## Performance

- Lightweight decorator-based routing
- Compiled to efficient JavaScript
- Built on Express performance
- Perfect for microservices

## References

- [OvernightJS Documentation](https://github.com/otoole-brian/overnight)
- [Express Middleware](https://expressjs.com/en/guide/using-middleware.html)
