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

344
Views
Node js express error hanlding middleware dont recognize 4 elements

I'm trying to create an error-handling middleware, and as you can see in the image attached when I'm making the

 app.use((err,req,res,next) => {...}) 

block my IDE is automatically recognized as 3 parameter middleware. and I can't get it to work as err-req-res-next middleware. I thought - maybe the IDE miss-indexed that block - and tried to run it, and while running the express doesn't recognize an error-handling middleware

3 parameters identified

I add the "app.use" in my server.js (which is the same as app.js), and as the last-placed app.use, right before the listen.

I'm using latest express to date: 4.18.1

Any help in finding a solution will be great!

Many thanks.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Add these two after all routing methods:

// 404 not found error
app.get('*', (req, res) => {
    res.status(404).send('<h1>error 404 not found</h1>');
})

// 500 server error
app.use((error, req, res, next)=>{
    if(error){
        res.send('500 OOPS :( Something went wrong... Please try again. ')
    }
})
over 4 years ago · Santiago Trujillo Report

0

Figured it out!

The issue was that I added the "error handling middleware" at the "server.js", it should have been in the placeholder.routes.js of the relevant controller.

abdurrahim answer really helped in figuring it out, after I added the code he attached I saw it was catching the errors, and the "500 server error" is, technically, an error-handling middleware.

So I added the following code after all the routes in the routes file, and managed to handle the different errors I was throwing.

Thanks a bunch!

enter image description here

over 4 years ago · Santiago Trujillo 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!