Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
When I'm making a POST requestion in my login page

I'm getting this error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client when I'm running my ExpressJS code.

Please help guys, this is my first question on stackoverflow btw and I don't really know how to ask questions :^)

My code:

app.route("/")

.get(function(req,res){
    res.render("home");
})

app.route("/register")
.get(function(req,res){
    res.render("register");
})

.post(function(req,res){

    bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
        const newUser = new User({
            email: req.body.username,
            password: hash
        })
    
        newUser.save(function(err){
            if (err){
                console.log(err);
            } else {
                res.render("secrets");
            }
        });
    });
});

app.route("/login")
.get(function(req,res){
    res.render("login");
})

.post(function(req,res){
    const username = req.body.username;
    const password = req.body.password;

    User.findOne({email: username}, function(err, foundUser){
        if (err){
            console.log(err);
        } else {
            if (foundUser){
                bcrypt.compare(password, foundUser.password, function(err,result){
                    if (result === true){
                        res.render("secrets");
                    }
                });
            }
        }
    });
    
});

app.listen(3000, function(){
    console.log("Server started...");
});

I got this error after I made a POST request in my login page, it was stuck on loading and It wasn't loading the secrets.ejs page. I think the error is in the login route, 'cause I'm getting that error when I'm making a POST request in login route.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda