function serverResponse(req, res) {

    const {
        method,
        url,
        headers
    } = req;

    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 === 'POST' && url === '/students') {

        console.log('Got a POST request on /students');
        res.setHeader('Content-type', 'applicaton/json');
        // Handle getting data from the client request (on POST)
        let data = []; // Used to collect chunks of data
        req.on('data', chunk => {
            // This event fires when we receive data in the request. The data comes in chunks
            // Only handle when we have the expected url
            if (url === '/students') {
                console.log(`Data chunk available: ${chunk}`);
                // We need to parse the chunk, or we will store it as a stream object
                data.push(JSON.parse(chunk));
            }
        });
        req.on('end', () => {

            if (data.length > 0) {
                console.log('retrieved data', data[0]);
                students.push(data[0].name);
                // Send the updated list of students in the response with a 201 status
                res.statusCode = 201;
                res.end(JSON.stringify(students));
            }
        });
    } else if (method === 'GET' && url === '/users') {
        fetch('https://jsonplaceholder.typicode.com/users')
            .then(res => res.json())
            .then(json => { 
                res.end(JSON.stringify(json)) // don't forget to stringify when returning to the client!
            });
    } else if (method === 'GET' && url === '/posts') {
        fetch('https://jsonplaceholder.typicode.com/posts')
            .then(res => res.json())
            .then(json => {
                const posts = json.map((post) => { // create an array of post titles with map!
                    return post.title
                })
                res.end(JSON.stringify(posts)) // don't forget to stringify when returning to the client!
            });
    }
    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,
        url,
        headers
    } = req;
    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 === 'POST' && url === '/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));
            }
        });
    } else if (method === 'GET' && url === '/users') {
        fetch('https://jsonplaceholder.typicode.com/users')
            .then(res => res.json())
            .then(json => { 
                res.end(JSON.stringify(json)) 
            });
    } else if (method === 'GET' && url === '/posts') {
        fetch('https://jsonplaceholder.typicode.com/posts')
            .then(res => res.json())
            .then(json => {
                const posts = json.map((post) => { 
                    return post.title
                })
                res.end(JSON.stringify(posts)) 
            });
    }
    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 , url , headers } = req ; 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 === 'POST' && url === '/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 ) ) ; } } ) ; } else if ( method === 'GET' && url === '/users' ) { fetch ( 'https://jsonplaceholder.typicode.com/users' ) . then ( res => res . json ( ) ) . then ( json => { res . end ( JSON . stringify ( json ) ) } ) ; } else if ( method === 'GET' && url === '/posts' ) { fetch ( 'https://jsonplaceholder.typicode.com/posts' ) . then ( res => res . json ( ) ) . then ( json => { const posts = json . map ( ( post ) => { return post . title } ) res . end ( JSON . stringify ( posts ) ) } ) ; } 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
// Handle getting data from the client request (on POST) // Used to collect chunks of data // This event fires when we receive data in the request. The data comes in chunks // Only handle when we have the expected url // We need to parse the chunk, or we will store it as a stream object // Send the updated list of students in the response with a 201 status // don't forget to stringify when returning to the client! // create an array of post titles with map! // don't forget to stringify when returning to the client!