function serverResponse(req, res) {

    // The data will be retrieved each time the server responds to a request:
    const data = getDataFromFileSystem('data.txt') // See function getDataFromFileSystem below
    const students = data.split('\n') // Split the data on line-breaks and return an array

    console.table(students)

    // 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('Data', data)
                        console.log('retrieved data', data[0]);
                        
                        students.push(data[0].name); // Add the name to the students array

                        // Write the students array to the file system:
                        const dataDump = createStringFromArray(students) // see `createStringFromArray()` function below
                        writeToFileSystem('data.txt', dataDump) // see `writeToFileSystem` function below

                        res.statusCode = 201; // http status 201: created
                        res.end(JSON.stringify(students)); // return the new array of students to the client
                    }
                });
                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 data = getDataFromFileSystem('data.txt') 
    const students = data.split('\n') 
    console.table(students)
    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('Data', data)
                        console.log('retrieved data', data[0]);
                        students.push(data[0].name); 
                        const dataDump = createStringFromArray(students) 
                        writeToFileSystem('data.txt', dataDump) 
                        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 data = getDataFromFileSystem ( 'data.txt' ) const students = data . split ( '\n' ) console . table ( students ) 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 ( 'Data' , data ) console . log ( 'retrieved data' , data [ 0 ] ) ; students . push ( data [ 0 ] . name ) ; const dataDump = createStringFromArray ( students ) writeToFileSystem ( 'data.txt' , dataDump ) 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
// The data will be retrieved each time the server responds to a request: // See function getDataFromFileSystem below // Split the data on line-breaks and return an array // 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 // Add the name to the students array // Write the students array to the file system: // see `createStringFromArray()` function below // see `writeToFileSystem` function below // http status 201: created // return the new array of students to the client // You can set a different response for invalid route for each method if you want