const http = require('http')
|
|
const url = require('url')
|
|
const { Client } = require('pg')
|
|
|
|
const server = http.createServer((req, res) => {
|
|
res.setHeader('Content-Type', 'text/html')
|
|
const baseURL = url.parse(req.url, true)
|
|
switch (baseURL.pathname) {
|
|
case '/salvar':
|
|
const {tipo, marca, publica} = baseURL.query;
|
|
const clientPG = new Client({
|
|
user: 'meu_site',
|
|
password: 'aRk9WtgJ',
|
|
database: 'meu_site',
|
|
host: 'localhost',
|
|
port: 5432
|
|
})
|
|
clientPG.connect((err) => {
|
|
if (err) {
|
|
console.error('Falha na conexão do PG:', err)
|
|
res.writeHead(500)
|
|
res.end('Falha interna no servidor.')
|
|
return
|
|
}
|
|
clientPG.query(
|
|
'INSERT INTO tipo_marca(tipo, marca, pode_publicar)'+
|
|
' VALUES(\''+ marca +'\', $1, $2)',
|
|
[ tipo, publica == 'true' ],
|
|
(err) => {
|
|
if (err) {
|
|
console.error('Falha na query de INSERT:', err)
|
|
res.writeHead(500)
|
|
res.end('Falha interna no servidor.')
|
|
return
|
|
}
|
|
res.writeHead(200)
|
|
res.end(`
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Salvar</title>
|
|
<link rel="stylesheet" href="/styles/base.css" />
|
|
</head>
|
|
<body>
|
|
<h1>Salvo com Sucesso</h1>
|
|
<p>
|
|
É uma simulação com NodeJS.
|
|
</p>
|
|
<p>Tipo: ${tipo}</p>
|
|
<p>Marca: ${marca}</p>
|
|
<p>Pode publicar? ${publica}</p>
|
|
</body>
|
|
</html>
|
|
`)
|
|
}
|
|
)
|
|
})
|
|
break;
|
|
default:
|
|
res.writeHead(404)
|
|
res.end('Não encontrado.')
|
|
break;
|
|
}
|
|
})
|
|
|
|
server.listen(3001, 'localhost')
|
|
|
|
console.log('Servidor rodando...')
|