0
I am making a nodejs application and came across the following error:
Cannot POST /news/news/save
how do I fix?
admin js.
module.exports = function(application){
application.get('/formulario_inclusao_noticia', (req, res) => {
res.render("admin/form_add_noticia");
});
application.post('/noticias/salvar', function(req,res){
var noticia = req.body;
console.log(noticia);
req.assert('titulo','Título é obrigatorio').notEmpty();
req.assert('resumo','Resumo é obrigatorio').notEmpty();
req.assert('resumo','Resumo é deve conter entre 10 e 100 caracteres').len(10, 100);
req.assert('autor', 'Autor obrigatório').notEmpty();
req.assert('data_noticia', 'Data inválida').notEmpty().isDate({format: 'YYYY-MM-DD'});
req.assert('noticia', 'Noticia obrigatório').notEmpty();
var erros = req.validationErrors();
console.log(erros);
if(erros){
res.render("admin/form_add_noticia");
return;
}
var connection = application.config.dbConnection();
var noticiasModel = new application.app.models.noticiasDAO(connection);
noticiasModel.salvarNoticia(noticia, (error, result) => {
res.redirect('/noticias');
});
});
}
newsDAO.js
function NoticiasDAO(connection) {
this._connection = connection;
}
NoticiasDAO.prototype.getNoticias = function(callback) {
this._connection.query("select * from noticias", callback);
}
NoticiasDAO.prototype.getNoticia = function(callback){
this._connection.query("select * from noticias where id_noticia = 3", callback);
}
NoticiasDAO.prototype.salvarNoticia = function(noticia, callback) {
console.log(noticia);
this._connection.query("insert into noticias set ? ", noticia, callback);
}
module.exports = () => {
return NoticiasDAO;
};
form
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Formulario de cadastro de Notícia</title>
</head>
<body>
<h1>Adicionar notícia</h1>
<br>
<form action="./noticias/salvar" method="post">
<label>Titulo da Noticia</label>
<input type="text" name="titulo" id="titulo" placeholder="Titulo da Notícia">
<br>
<label>Detalhes da Notícia: </label>
<textarea name="noticia" id="noticia" cols="30" rows="5"></textarea>
<br>
<label>Resumo</label>
<input type="text" name="resumo" id="resumo" placeholder="Titulo da Notícia">
<br>
<label>Data dos Fatos</label>
<input type="date" name="data_noticia" id="data_noticia" placeholder="Titulo da Notícia">
<br>
<label>Nome do Autor</label>
<input type="text" name="autor" id="autor" placeholder="Titulo da Notícia">
<br>
<input type="submit" value="Enviar">
</form>
</body>
I believe the mistake is in either of these two.
Provide complete error message
– Giovane
The url you registered was
/noticias/salvar
, and you’re trying to call/noticias/noticias/salvar
...– Giovane
I put the image there. I know you’re giving some trouble with the routes, but I can’t identify which
– user215274
It seems to me you’re repeating the path
/noticias
– Giovane
yes, I am. but what I couldn’t identify was where. because this only occurs when sending the form
– user215274
The problem is in sending your form, and you didn’t put it in the question..
– Giovane
All right, there you go
– user215274
Let’s go continue this discussion in chat.
– user215274