Guião para a quarta semana de aulas (RPCW2023)

Gestão de alunos

Tarefas a realizar na aula:

  1. Apresentação sobre HTML Forms: Slides no drive

  2. Dataset da UC:

  3. Criar uma App Web com as seguintes funcionalidades:

  4. Criar uma pasta public para os recursos estáticos:

  5. Criar as templates das páginas em falta;

  6. Tratamento do POST:

case "POST": if(req.url == '/alunos'){ recuperaInfo(req, resultado => { resultado['id'] = resultado['Id'] console.log('POST de aluno:' + JSON.stringify(resultado)) axios.post('http://localhost:3000/alunos', resultado) .then(resp => { res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'}) res.write(geraPostConfirm( resp.data, d)) res.end() }) .catch(erro => { res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'}) res.write('<p>Erro no POST: ' + erro + '</p>') res.write('<p><a href="/">Voltar</a></p>') res.end() }) }) }
  1. Recuperar a informação do body dum pedido:

var {parse} = require('querystring') // Aux function to process body function collectRequestBodyData(request, callback) { if(request.headers['content-type'] === 'application/x-www-form-urlencoded') { let body = ''; request.on('data', chunk => { body += chunk.toString(); }); request.on('end', () => { callback(parse(body)); }); } else { callback(null); } }
  1. Utilização da função anterior:
case "POST": { // POST /persons ------------------------------------------------------------------- collectRequestBodyData(req, result => { if(result){ console.dir(result) res.writeHead(201, {'Content-Type': 'text/html;charset=utf-8'}) res.write(formTemplates.personPostConfirmPage(result, d)) res.end() } else{ res.writeHead(201, {'Content-Type': 'text/html;charset=utf-8'}) res.write("<p>Unable to collect data from body...</p>") res.end() } }); break }
  1. Formulário para o aluno:
// Template para o formulário de aluno ------------------ function geraFormAluno( d ){ return ` <html> <head> <title>Registo de um aluno</title> <meta charset="utf-8"/> <link rel="icon" href="favicon.png"/> <link rel="stylesheet" href="w3.css"/> </head> <body> </body> <div class="w3-container w3-teal"> <h2>Registo de Aluno</h2> </div> <form class="w3-container" action="/alunos" method="POST"> <label class="w3-text-teal"><b>Nome</b></label> <input class="w3-input w3-border w3-light-grey" type="text" name="Nome"> <label class="w3-text-teal"><b>Número / Identificador</b></label> <input class="w3-input w3-border w3-light-grey" type="text" name="Id"> <label class="w3-text-teal"><b>Link para o repositório no Git</b></label> <input class="w3-input w3-border w3-light-grey" type="text" name="Git"> <input class="w3-btn w3-blue-grey" type="submit" value="Registar"/> <input class="w3-btn w3-blue-grey" type="reset" value="Limpar valores"/> </form> <footer class="w3-container w3-teal"> <address>Gerado por galuno::RPCW2022 em ${d}</address> </footer> </body> </html> ` }