Rock-Paper-Scissors (Node.js) Completed

Hey guys, I just completed Node.js final project Rock-Paper-Scissors.

Since there’s no solution to this currently, I will just post mine if you guys want to refer to it. Tell me if there’s anything wrong.

const fs = require('fs');
// TODO: Require the http module
const http = require('http');

// TODO: Create a server
const server = http.createServer((request, response) => {

  const url = new URL(request.url, `http:${request.headers.host}`);

  switch(url.pathname) {
    case '/':
      if (request.method === 'GET') {
        const name = url.searchParams.get('name');
        console.log(name);

        response.writeHead(202, {'Content-Type': 'text/html'});
        fs.createReadStream('./index.html').pipe(response);
        break;
      } else if(request.method === 'POST') {
        handlePostResponse(request, response);
        break;
      }
      break;

    default:
      response.writeHead(404, {'Content-Type': 'text/html'});
      fs.createReadStream('./404.html').pipe(response);
      break;
  }
})

server.listen(4001, () => {
  console.log("Server is listening at " + server.address().port);
})

// Function for handling POST responses
function handlePostResponse(request, response){
  request.setEncoding('utf8');
  
  // Receive chunks on 'data' event and concatenate to body variable
  let body = '';
  request.on('data', function (chunk) {
    body += chunk;
  });
  
  // When done receiving data, select a random choice for server
  // Compare server choice with player's choice and send an appropriate message back
  request.on('end', function () {
    const choices = ['rock', 'paper', 'scissors'];
    const randomChoice = choices[Math.floor(Math.random() * 3)];

    const choice = body;

    let message;

    const tied = `Aww, we tied! I also chose ${randomChoice}.`;
    const victory = `Dang it, you won! I chose ${randomChoice}.`;
    const defeat = `Ha! You lost. I chose ${randomChoice}.`;

    if (choice === randomChoice) {
      message = tied;
    } else if (
        (choice === 'rock' && randomChoice === 'paper') ||
        (choice === 'paper' && randomChoice === 'scissors') ||
        (choice === 'scissors' && randomChoice === 'rock')
    ) {
      message = defeat;
    } else {
      message = victory;
    }
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end(`You selected ${choice}. ${message}`);
  });
}
2 Likes

Hi, thank you, I understood were I’ve got wrong close brackets. Your solution is looking good. Only one small thing:

default:
      response.writeHead(404, 'Page is not found');

@eugenegoh Thanks for posting this. It allowed me to debug my program. Strangely, when I click ‘refresh browser’ nothing happens. I have the server running after launching app.js. My code matches yours verbatim. Am I missing something?

Hi, I have this code, but there is something wrong, I can´t load the html…
Can you help me please…

const fs = require('fs');
// TODO: Require the http module
const http = require('http');

// TODO: Create a server

const server = http.createServer((request, response) =>{

  const url = new URL(request.url, `http://${request.headers.host}`);

  switch(url.pathname){
    case '/':
    if(request.method === 'GET'){
      const name = url.searchParams.get('name');
      console.log(`El usuario ${name} ha empezado a jugar`);
      response.writeHead(200, {
        'Content-Type':'text/html'
      });
      fs.createReadStream('./index.html').pipe(response);
      break;

    }
    else if(url.method === 'POST'){
      handlePostResponse(request, response);
      break;


    }
    default:
    response.writeHead(400, {
      'Content-Type' : 'text/html'
    });
    
    fs.createReadStream('./404.html').pipe(response);
    break;

  }


});

server.listen(4001, ()=>{

  console.log(`Server is listening on http://localhost${server.address().port}`);
});

  // TODO: Create a url object with request url and host name

  // TODO: Create a switch statement based on pathname of url

      // TODO: Check if request method is GET

        // TODO: Get value of 'name' query

        // TODO: Write response header

        // TODO: Pipe index.html to response


      // TODO: Check if request is POST and if so, run handlePostResponse()

      // TODO: Write response header

      // TODO: Pipe 404.html to response


// TODO: Have server listen at port 4001


// Function for handling POST responses
function handlePostResponse(request, response){
  request.setEncoding('utf8');
  
  // Receive chunks on 'data' event and concatenate to body variable
  let body = '';
  request.on('data', function (chunk) {
    body += chunk;
  });
  
  // When done receiving data, select a random choice for server
  // Compare server choice with player's choice and send an appropriate message back
  request.on('end', function () {
    const choices = ['rock', 'paper', 'scissors'];
    const randomChoice = choices[Math.floor(Math.random() * 3)];

    const choice = body;

    let message;

    const tied = `Aww, we tied! I also chose ${randomChoice}.`;
    const victory = `Dang it, you won! I chose ${randomChoice}.`;
    const defeat = `Ha! You lost. I chose ${randomChoice}.`;

    if (choice === randomChoice) {
      message = tied;
    } else if (
        (choice === 'rock' && randomChoice === 'paper') ||
        (choice === 'paper' && randomChoice === 'scissors') ||
        (choice === 'scissors' && randomChoice === 'rock')
    ) {
      message = defeat;
    } else {
      message = victory;
    }
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end(`You selected ${choice}. ${message}`);
  });
}

I have the follow error:

<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Rock-Paper-Scissors</title>
</head>
<body>
<h1>404 Page Not Found!</h1>
<a href="/">Home</a>

<script type="text/javascript" src="https://external-production.codecademy.com/assets/relay.js"></script></body></html>