function serverResponse(req, res) {

    // Get the http method, url, and headers from the request
    const {
        method,
        url,
        headers
    } = req;

    console.log(`Method: ${method}, URL: ${url}, Headers: ${headers}`)
    // handle the routes

    if (method === 'GET') { // Use if and else if to handle the method since there are a finite amount of them
        switch (url) { // Use switch to handle routes because it's easier to read and there could be LOTS! 
            case '/':
                console.log('Getting a random pair of students');
                res.setHeader('Content-type', 'text/plain');
                res.end(randomPair());
                break;

            case '/students':
                console.log("Getting a list of students");
                res.setHeader('Content-type', 'application/json');
                res.end(JSON.stringify(students));
                break;

            default: // You can set a different response for invalid route for each method if you want
                invalidRequest('Invalid GET request.')
                break;
        }
    } else if (method === 'POST') {
        switch (url) {
            case '/students':
                console.log('Got a POST request on /students');
                res.setHeader('Content-type', 'applicaton/json');

                let data = [];
                req.on('data', chunk => {
                    if (url === '/students') {
                        console.log(`Data chunk available: ${chunk}`);
                        data.push(JSON.parse(chunk));
                    }
                });
                req.on('end', () => {
                    if (data.length > 0) {
                        console.log('retrieved data', data[0]);
                        students.push(data[0].name);
                        res.statusCode = 201;
                        res.end(JSON.stringify(students));
                    }
                });
                break;
            default: // You can set a different response for invalid route for each method if you want
                invalidRequest(req, res, 'Invalid POST request.')
                break;
        }
    } else {
        console.log('Got an invalid method or route');
        invalidRequest('Invalid request and url.')
    }
}
// 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,
        url,
        headers
    } = req;
    console.log(`Method: ${method}, URL: ${url}, Headers: ${headers}`)
    if (method === 'GET') { 
        switch (url) { 
            case '/':
                console.log('Getting a random pair of students');
                res.setHeader('Content-type', 'text/plain');
                res.end(randomPair());
                break;
            case '/students':
                console.log("Getting a list of students");
                res.setHeader('Content-type', 'application/json');
                res.end(JSON.stringify(students));
                break;
            default: 
                invalidRequest('Invalid GET request.')
                break;
        }
    } else if (method === 'POST') {
        switch (url) {
            case '/students':
                console.log('Got a POST request on /students');
                res.setHeader('Content-type', 'applicaton/json');
                let data = [];
                req.on('data', chunk => {
                    if (url === '/students') {
                        console.log(`Data chunk available: ${chunk}`);
                        data.push(JSON.parse(chunk));
                    }
                });
                req.on('end', () => {
                    if (data.length > 0) {
                        console.log('retrieved data', data[0]);
                        students.push(data[0].name);
                        res.statusCode = 201;
                        res.end(JSON.stringify(students));
                    }
                });
                break;
            default: 
                invalidRequest(req, res, 'Invalid POST request.')
                break;
        }
    } else {
        console.log('Got an invalid method or route');
        invalidRequest('Invalid request and url.')
    }
}
function serverResponse ( req , res ) { const { method , url , headers } = req ; console . log ( ` ${ method } ${ url } ${ headers } ` ) if ( method === 'GET' ) { switch ( url ) { case '/' : console . log ( 'Getting a random pair of students' ) ; res . setHeader ( 'Content-type' , 'text/plain' ) ; res . end ( randomPair ( ) ) ; break ; case '/students' : console . log ( "Getting a list of students" ) ; res . setHeader ( 'Content-type' , 'application/json' ) ; res . end ( JSON . stringify ( students ) ) ; break ; default : invalidRequest ( 'Invalid GET request.' ) break ; } } else if ( method === 'POST' ) { switch ( url ) { case '/students' : console . log ( 'Got a POST request on /students' ) ; res . setHeader ( 'Content-type' , 'applicaton/json' ) ; let data = [ ] ; req . on ( 'data' , chunk => { if ( url === '/students' ) { console . log ( ` ${ chunk } ` ) ; data . push ( JSON . parse ( chunk ) ) ; } } ) ; req . on ( 'end' , ( ) => { if ( data . length > 0 ) { console . log ( 'retrieved data' , data [ 0 ] ) ; students . push ( data [ 0 ] . name ) ; res . statusCode = 201 ; res . end ( JSON . stringify ( students ) ) ; } } ) ; break ; default : invalidRequest ( req , res , 'Invalid POST request.' ) break ; } } else { console . log ( 'Got an invalid method or route' ) ; invalidRequest ( 'Invalid request and url.' ) } }
Callback function that will handle all of our server functions
Callback function that will handle all of our server functions
// Get the http method, url, and headers from the request // handle the routes // Use if and else if to handle the method since there are a finite amount of them // Use switch to handle routes because it's easier to read and there could be LOTS!  // You can set a different response for invalid route for each method if you want // You can set a different response for invalid route for each method if you want