jcramalho

PRI2019::Aula4::Servidor

Oct 21st, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http')
  2. var pug = require('pug')
  3. var fs = require('fs')
  4. var jsonfile = require('jsonfile')
  5. var {parse} = require('querystring')
  6.  
  7. var myBD = 'alunos.json'
  8.  
  9. var myserver = http.createServer(function (req, res) {
  10.     console.log(req.method + ' ' + req.url)
  11.  
  12.     if(req.method == 'GET'){
  13.         if(req.url == '/w3.css'){
  14.             fs.readFile('w3.css', (erro, dados)=>{
  15.                 if(!erro){
  16.                     res.writeHead(200, {'Content-Type': 'text/css'})
  17.                     res.write(dados)
  18.                 }    
  19.                 else {
  20.                     res.writeHead(200, {'Content-Type': 'text/plain'})
  21.                     res.write('Erro na leitura do w3.css...')
  22.                 }    
  23.                 res.end()
  24.             })
  25.         }
  26.         else if(req.url == '/'){
  27.             res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
  28.             res.write(pug.renderFile('index.pug'))
  29.             res.end()
  30.         }
  31.         else if(req.url == '/alunos'){
  32.             fs.readFile(myBD, (erro, dados) => {
  33.                 if(!erro){
  34.                     res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
  35.                     res.write(pug.renderFile('lista-alunos.pug',
  36.                             {lista: JSON.parse(dados)}))
  37.                     res.end()
  38.                 }
  39.                 else{
  40.                     res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'})
  41.                     res.end('Erro: ' + erro)  
  42.                 }
  43.             })      
  44.         }
  45.         else if(req.url == '/registar'){
  46.             res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
  47.             res.write(pug.renderFile('form-aluno.pug'))
  48.             res.end()
  49.         }
  50.         else{
  51.             res.end('Erro: pedido não suportado [' + req.url + ']')
  52.         }
  53.     }
  54.     else if(req.method == 'POST'){
  55.         if(req.url == '/alunos'){
  56.             recuperaInfo(req, resultado => {
  57.                 jsonfile.readFile(myBD, (erro, alunos)=>{
  58.                     if(!erro){
  59.                         alunos.push(resultado)
  60.                         // console.dir(alunos)
  61.                         jsonfile.writeFile(myBD, alunos, erro => {
  62.                             if(erro) console.log(erro)
  63.                             else console.log('Registo gravado com sucesso.')
  64.                         })
  65.                     }
  66.                 })
  67.                 res.end(pug.renderFile('aluno-recebido.pug', {aluno: JSON.stringify(resultado)}))
  68.             })
  69.         }
  70.         else{
  71.             res.end('Erro: pedido não suportado [' + req.url + ']')
  72.         }
  73.     }
  74.     else{
  75.         res.end('Erro: método não suportado [' + req.method + ']')
  76.     }
  77. })
  78.    
  79. myserver.listen(3021)
  80.  
  81. console.log('Servidor à escuta na porta 3021...')
  82.  
  83. function recuperaInfo(request, callback){
  84.     if(request.headers['content-type'] == 'application/x-www-form-urlencoded'){
  85.         let body = ''
  86.         request.on('data', bloco => {
  87.             body += bloco.toString()
  88.         })
  89.         request.on('end', ()=>{
  90.             callback(parse(body))
  91.         })
  92.     }
  93.     else callback(null)
  94. }
Advertisement
Add Comment
Please, Sign In to add comment