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

202
Views
how to break in middleware in expressjs

I have a MEAN stack based web application and I have a middle ware which validates the request for some authorization purpose. I have written the security checking part but if the user is unauthorized, the express middle ware should stop execution and return back user 401 status with a message. How can I achieve it? I tried to to use return statement in middle ware but it is not working. Though some of them works, it throws an exception and breaks the server. I want just an abrupt ending to the request. Any help will be appreciated.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Assuming that isValid is a hypothetical function to check the validity of the request.

function authMiddleware(req,res,next){
  if(isValid(req)){
   next(); // calls the next middleware
  }else {
   return res.sendStatus(401); // sends HTTP 401 back
  }
}
about 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!