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

271
Visualizações
how to prevent freezing get route on nodejs expresjs

I have a route like http://localhost:3000/admin/video/edit/5 and the controller looks like this

 albumEdit: async (req, res) => {
        const editInfoId = req.params.id;
        await Movie.findOne({ where: { id: editInfoId } }).then((movie) => {
            if (movie) {
                res.render('admin/movies/edit', { title: 'Edit Movie On Page One', movie });
            }
        });
    },

for the testing purpose when I type the wrong id after edit/ then the process is freezing after some time I am getting 500 errors.

how to prevent this if someone tries to break my app with the wrong id in the URL? I want something like if anyone tries to do this application redirect to an error page.

I am new in node js express js I need some info.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your route will freeze if movie is falsy or if fineOne results in an error because for both of these cases you don't send any response.

after some time I am getting 500 errors.

If you run your node server behind a web server then this 500 is due to a timeout because your router does not send a response.

how to prevent this if someone tries to break my app with the wrong id in the URL? I want something like if anyone tries to do this application redirect to an error page.

As with any programming language or code, make sure you handle all control flows and possible exceptions.

Besides that, if you use await you in most of the cases don't want to use .then.

   albumEdit: async (req, res) => {
     const editInfoId = req.params.id;

     try {
       let movie = await Movie.findOne({
         where: {
           id: editInfoId
         }
       })

       if (movie) {
         res.render('admin/movies/edit', {
           title: 'Edit Movie On Page One',
           movie
         });
       } else {
         // either the if is not necessary or you have to also handle the else cases
         
         // send some error response
         res.send('error')
       }
     } catch (err) {
       // send some error response
       res.send('error')
     }
  }

For completeness, this is how where you would need to do changes in your code, but as said above don't mix await and then:

 
   albumEdit: async (req, res) => {
     const editInfoId = req.params.id;

     try {
       await Movie.findOne({
         where: {
           id: editInfoId
         }
       }).then((movie) => {
         if (movie) {
           res.render('admin/movies/edit', {
             title: 'Edit Movie On Page One',
             movie
           });
         } else {
           // either the if is not necessary or you have to also handle the else cases
           
           // send some error response
           res.send('error')
         }
       });
     } catch (err) {
        // send some error response
        res.send('error')
     }
   }
 
about 4 years ago · Juan Pablo Isaza Relatório
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