Skip to content

Instantly share code, notes, and snippets.

@nathabonfim59
Created July 21, 2017 01:38
Show Gist options
  • Save nathabonfim59/5be224178fee18bd1a79edf6d1cf22cb to your computer and use it in GitHub Desktop.
Save nathabonfim59/5be224178fee18bd1a79edf6d1cf22cb to your computer and use it in GitHub Desktop.
FullStackAcademy - aula 1 - exercício 6
const express = require('express')
const app = express()
const porta = 3000
// Redireciona o usuário para o a página de soma
app.get('/', (request, response) => {
response.send('Vá para <a href="/somar">Somar</a>')
})
// Realiza a soma propriamente dita com base nas variáveis na URL
app.get('/somar', (request, response) => {
if (request.query) {
if (request.query.num1 && request.query.num2) {
response.send('A soma é: '+(Number(request.query.num1) + Number(request.query.num2)))
} else {
response.send('Digite os números na url. <br> <strong>Exemplo</strong><br>\
O exemplo abaixo soma os algarismos 50 + 9, some os seus também!<br>\
<a href="http://localhost:'+porta+'/somar?num1=50&num2=9">\
http://localhost:'+porta+'/somar?num1=50&num2=9</a>')
}
}
})
// Inicia o servidor na porta 3000
app.listen(porta, () => console.log('O servidor está rodando em: http://localhost:'+porta))
@tuliofaria
Copy link

Certinho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment