Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda