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

186
Views
What the correct way to exit an Express app.all

This is my app.all. Basically, I'm calling a fetchBuildings function based on the building ID/Hash, then setting title, description, and image based on the response:

app.all('/:id', function (req, res) {
  const hash = req.params.id
  const obj = {}
  if (hash === 'undefined') {
    obj.title = 'iStaging LiveTour'
    obj.description = ''
    obj.image = 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
    return
  }
  fetchBuildingsByHash(hash).then(({title, description, image, isBasicPlan}) => {
    if (isBasicPlan) {
      obj.title = 'iStaging LiveTour'
      obj.description = ''
      obj.image = 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
    } else {
      obj.title = title || 'iStaging LiveTour'
      obj.description = description || ''
      obj.image = image || 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
    }
    res.render('index.ejs', obj)
  }).catch((err) => {
    const obj = {
      title: 'notFound'
    }
    res.render('404.ejs', obj)
  })
});

Sometimes hash is 'undefined' so I want to stop the code when that happens.

I'm just using return here, but I wonder if this is the conventional way of doing it. Is there another more 'proper' way?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should always either return a response, or pass the request along the middleware chain. If you just return, the request will get "stuck": the client will keep on waiting for a response that never comes, and eventually it will time out.

Let's assume that passing an hash of undefined is considered invalid. You could return a 400 ("Bad Request") response in that case:

if (hash === 'undefined') {
  return res.sendStatus(400);
}

If you want to pass the request along, which will likely result in a 404 ("Not Found") response being returned by Express:

app.all('/:id', function (req, res, next) {
  const hash = req.params.id
  const obj = {}
  if (hash === 'undefined') {
    return next();
  }
  ...
})

Or explicitly pass an error, resulting in a 500 ("Internal Server Error") response returned by Express:

if (hash === 'undefined') {
  return next(Error('invalid hash'));
}
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!