1
Hello, I’m trying to post the data of a form using Express, follow the code below
index.html
<form action="insere" method="post">
<p><h4> Título </h4> <input type="text" name="titulo" id="titulo" size="40" maxlength="30" placeholder="Digite o título da tarefa"/> </p>
<p><h4>Descrição</h4> <textarea name="descricao" id="descricao" cols="50" rows="3" placeholder="Deixe uma breve descrição sobre sua tarefa"></textarea> </p>
<p>
<h4>Grau de importância(quanto maior o número, maior a importância)</h4>
<input type="radio" name="urgencia" value="2"> Muito
<input type="radio" name="urgencia" value="1" checked> Médio
<input type="radio" name="urgencia" value="0"> Pouco
</p>
<p>
<h4>Como irá realizar essa tarefa</h4>
<select name="tipoTarefa" id="select">
<option value="2"> Número vezes </option>
<option value="1"> Hora </option>
<option value="0"> Minuto </option>
</select>
<input type="text" name="numeroRepeticoes" id="options" size="40" maxlength="5" placeholder="Em quantas vezes você pretende fazer?"/> </p>
</p>
<p><button type="submit"> Criar </button></p>
</form>
app js.
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }))
const db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'metas'
});
db.connect( (err) => {//Conectando
if(err) throw err;
console.log('MySQL conectado...');
});
app.get('/select', (req, res) => {
let sql = "SELECT * FROM tarefas";
db.query(sql, (err, result) => {
if(err) throw err;
res.send(result);
})
})
app.post('/insere', (req, res) => {
let post =
{titulo: req.body.titulo,
descricao: req.body.descricao,
grau: req.body.urgencia,
tipoRealizacao: req.body.tipoTarefa,
repeticoes: req.body.numeroRepeticoes
}
let sql = 'INSERT INTO tarefas SET ?';
let query = db.query(sql, post, (err, result) => {
if(err) throw err;
res.send("Post added");
})
})
app.listen('3000', () => { console.log("Server iniciado") } );
As you can see I’m using mysql, I’m also using wampp, on port 3306, the point is that when I give Submit in the form it says that the requested URL is not on that server, it’s as if it can’t go to localhost:3000 which is where the app is listening, Can someone please help me? I don’t know if it might make a difference but the index is on the public page and the app is in the src folder. Thank you
Have you tried it with
action="/insere"
?– Sergio
Yes, I’ve tried many such things
– Jefferson Rodrigues
How are you rendering your index?
post
as equal toreq.body
and gives aconsole.log(post)
, see if fields are coming in the variable– Joao Benthin