Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!