---
# System prepended metadata

title: H09A Tut05 Express

---

# H09A Tut05 Express

## Starter Code

- For tutor to modify

```ts
import express, { json, Request, Response } from 'express';
import morgan from 'morgan';
import { port, url } from './config.json';
import {
  clear,
  addNameAge,
  editNameAge,
  getNamesAges,
  getStats,
  removeNameAge,
} from './names.ages';

const app = express();

app.use(json());
// For debugging purposes - logs http requests
app.use(morgan('dev'));

// TODO TUTOR 0: /clear

// TODO GROUP 1: /addnameage

// TODO GROUP 2: /getnamesages

// TODO GROUP 3: /editnameage

// TODO GROUP 4: /removenameage

// TODO GROUP 5: /getstats

app.listen(port, () => {
  console.log(`Starting Express Server at the URL: '${url}:${port}'`);
});
```


## Tutor 0 - /clear

- Method: `DELETE`
- Parameter Type: `Query`

```ts
app.delete('/clear', (req: Request, res: Response) => {
    res.json(clear());
});
```

---

## Group 1 - /addnameage

- Method: `POST`
- Parameter Type: `Body`
```ts
app.post('/addnameage', (req: Request, res: Response) => {
    const {name, age} = req.body;
    res.json(addNameAge(name, age ));
});
    
```

---

## Group 2 - /getnamesages

- Method: `GET`
- Parameter Type: `Query`

```ts
app.get('/getnameages', (req: Request, res: Response) => {
    res.json(getNamesAges(req.query.minAge));
})
```

---

## Group 3 - /editnameage

- Method: PUT
- Parameter Type: `body`

```ts
app.put('/editnameage', (req: Request, res: Response) => {
  const { name, age  } = req.body  
  res.json(editNameAge(name, age))
});
```

---

## Group 4 - /removenameage

- Method: `DELETE`
- Parameter Type: `query`
- 
```

app.delete('/removenameage', (req: Request, res: Response) => {
  const name = req.query.nameAge as string;
  res.json(removeNameAge(name));
  console.log('Removing', name, 'from data');
});
```
---

## Group 5 - /getstats

- Method: `GET`
- Parameter Type: `Q`

```ts
console.log('TODO')
app.get('/getstats', (req: Request, res: Response) => {
    res.json(getStats())
})
```




