function serverResponse(req, res) {

    const {
        method,
        headers
    } = req;

    const url = urlLib.parse(req.url) // Parse the url using the url module
    console.log(url)
    const query = queryString.parse(url.query) // Parse the query string using the querystring module
    console.log(query)

    if (method === 'GET' && url === '/') {
        console.log('Getting a random pair of students');
        res.setHeader('Content-type', 'text/plain');
        res.end(randomPair());
    } else if (method === 'GET' && url === '/students') {
        console.log("Getting a list of students");
        res.setHeader('Content-type', 'application/json');
        res.end(JSON.stringify(students));
    } else if (method === 'GET' && url.pathname === '/students/') {
        res.setHeader('Content-type', 'application/json')
        filteredStudents = students.filter((value) => {
            return value === query.filter
        })
        res.end(JSON.stringify(filteredStudents))
    }
    else {
        console.log('Got an invalid method or route');
        res.statusCode = 404;
        res.setHeader('Content-type', 'text/plain');
        res.end('Route not found');
    }
}
// Callback function that will handle all of our server functions
Callback function that will handle all of our server functions
Callback function that will handle all of our server functions
function serverResponse(req, res) {
    const {
        method,
        headers
    } = req;
    const url = urlLib.parse(req.url) 
    console.log(url)
    const query = queryString.parse(url.query) 
    console.log(query)
    if (method === 'GET' && url === '/') {
        console.log('Getting a random pair of students');
        res.setHeader('Content-type', 'text/plain');
        res.end(randomPair());
    } else if (method === 'GET' && url === '/students') {
        console.log("Getting a list of students");
        res.setHeader('Content-type', 'application/json');
        res.end(JSON.stringify(students));
    } else if (method === 'GET' && url.pathname === '/students/') {
        res.setHeader('Content-type', 'application/json')
        filteredStudents = students.filter((value) => {
            return value === query.filter
        })
        res.end(JSON.stringify(filteredStudents))
    }
    else {
        console.log('Got an invalid method or route');
        res.statusCode = 404;
        res.setHeader('Content-type', 'text/plain');
        res.end('Route not found');
    }
}
function serverResponse ( req , res ) { const { method , headers } = req ; const url = urlLib . parse ( req . url ) console . log ( url ) const query = queryString . parse ( url . query ) console . log ( query ) if ( method === 'GET' && url === '/' ) { console . log ( 'Getting a random pair of students' ) ; res . setHeader ( 'Content-type' , 'text/plain' ) ; res . end ( randomPair ( ) ) ; } else if ( method === 'GET' && url === '/students' ) { console . log ( "Getting a list of students" ) ; res . setHeader ( 'Content-type' , 'application/json' ) ; res . end ( JSON . stringify ( students ) ) ; } else if ( method === 'GET' && url . pathname === '/students/' ) { res . setHeader ( 'Content-type' , 'application/json' ) filteredStudents = students . filter ( ( value ) => { return value === query . filter } ) res . end ( JSON . stringify ( filteredStudents ) ) } else { console . log ( 'Got an invalid method or route' ) ; res . statusCode = 404 ; res . setHeader ( 'Content-type' , 'text/plain' ) ; res . end ( 'Route not found' ) ; } }
Callback function that will handle all of our server functions
Callback function that will handle all of our server functions
// Parse the url using the url module // Parse the query string using the querystring module