Cannot POST nodejs

Asked

Viewed 126 times

0

I am making a nodejs application and came across the following error:

Cannot POST /news/news/save

erro

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

  • 1

    The url you registered was /noticias/salvar, and you’re trying to call /noticias/noticias/salvar...

  • I put the image there. I know you’re giving some trouble with the routes, but I can’t identify which

  • It seems to me you’re repeating the path /noticias

  • yes, I am. but what I couldn’t identify was where. because this only occurs when sending the form

  • The problem is in sending your form, and you didn’t put it in the question..

  • All right, there you go

Show 3 more comments

1 answer

0


the error is in the "action" form

"./news/save" is relative it will be converted to "{current url}/news/save"

the solution is simple: delete the point, and it will become an absolute URL: "/news/save" which will be converted by the browser to "{site}/news/save" (instead of "{site}/news/news/save")

  • that’s right. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.