---
# System prepended metadata

title: 23T3 tut05 functions

---

# 23T1 tut05 functions

```javascript=
import express, { json, Request, Response } from 'express';
import cors from 'cors';
import morgan from 'morgan';
import { port, url } from './config.json';
import {
  clear,
  peopleAdd,
  personView,
  personEdit,
  peopleList,
  personRemove,
  peopleStats,
} from './people';

const app = express();

// Use middleware that allows for access from other domains (needed for frontend to connect)
app.use(cors());
// Use middleware that allows us to access the JSON body of requests
app.use(json());
// Use middleware to log (print to terminal) incoming HTTP requests (OPTIONAL)
app.use(morgan('dev'));

app.get('/', (req: Request, res: Response) => {
  res.json({ message: 'Welcome to the root URL of names ages!' });
});

app.method('/path', (json comms params) => {
    // get your input
    // call your function - store the o/p in a variable
    // check for errors in your func o/p
    // pass it as json o/p
})

// **TODO TUTOR 0: /clear**
// PATH: /clear
// HTTP METHOD: delete
// INPUT PARAMETER TYPE: QUERY
// FUNC NAME: clear()
// FUNC PARAMETERS: -
// FUNC RETURNS: {}
// WRAPPER:
app.delete('/clear', (req: Request, res: Response) => {
    res.json(clear())
    
    // or
    
    const result = clear();

    if ('error' in result) {
        return res.status(400).json(result)
    }
    
   res.json(result)
    
    // OR
    
    // const my_result = clear();
    // assuming it has value:
    const my_result = {
        status: 400
        json: 'erroorrr'
    }

    const my_result1 = {
        status: 200
        json: {
            id: 2
        }
    }
    
    res.status(my_result.status).json(my_result.json)
    res.status(my_result1.status).json(my_result1.json)
    
})

// Boost: /people/add**
// PATH: 
// HTTP METHOD: post
// INPUT PARAMETER TYPE: name: string, age: number
// PARAMETERS: 
// FUNC RETURNS: {} or { error: string }
// WRAPPER: 
app.post('/people/add/, (req: Request, res: Response) => {
    const { name, age } = req.body;
    const result = peopleAdd(name, age);
    if ('error' in result) {
        res.status(400);
    }
         
    res.json(result);   
})

// AERO: /people/list
// PATH: /people/list
// HTTP METHOD: GET
// INPUT PARAMETER TYPE: query
// PARAMETERS: minAge
// RETURNS: Object with people : Array(that contains an array of all people)
/*
{
  "people": [
    {
      "personId": 1531,
      "name": "Tammy McTamtam",
      "age": 22
    }
  ]
}
*/
// WRAPPER:
app.get('people/list', (req: Request, res: Response) => {
    
    
    const minAge = parseInt(req.query.minAge);
    const result = peopleList(minAge);
    
    if ('error' in result) {
        return res.status(400).json(result)
    }
    
    res.status(200).json(result);
    
});


// **TODO GROUP 3: /people/:peopleid**
// PATH:
// HTTP METHOD:
// INPUT PARAMETER TYPE: 
// PARAMETERS: 
// RETURNS: --do we need this here, if not, then in which file do we care about them?
// WRAPPER: 

// **TODO GROUP 4: /people/:peopleid**
// PATH:
// HTTP METHOD:
// INPUT PARAMETER TYPE: 
// PARAMETERS: 
// RETURNS: --do we need this here, if not, then in which file do we care about them?
// WRAPPER: 

// **TODO GROUP 5: /people/:peopleid**
// PATH:
// HTTP METHOD:
// INPUT PARAMETER TYPE: 
// PARAMETERS: 
// RETURNS: --do we need this here, if not, then in which file do we care about them?
// WRAPPER: 

// **TODO BONUS: /people/stats**
// PATH:
// HTTP METHOD:
// INPUT PARAMETER TYPE: 
// PARAMETERS: 
// RETURNS: --do we need this here, if not, then in which file do we care about them?
// WRAPPER: 

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