1

I'm using a function for multiple times by calling from different routes. It's running but express/node.js not handling errors in the function.

Here's the example.

# routes.js
var app = express()
var { test } = require('../functions/test.js')(app)

router.get('/test', (req, res) => {
  console.log(test())
})
# test.js
function test() {
  return 'Hey'
}

module.exports = function(app){
  return { test }
}

If I call a function in test() which is not exists, It's not throwing errors and returns nothing inside console.log(test())

# test.js
function test() {
  heyHeyheyHey()
  return 'Hey'
}

module.exports = function(app){
  return { test }
}

Do I have to define this module in somewhere or something?

Taner Tunçer
  • 89
  • 1
  • 8
  • What is the error? – hoangdv Oct 31 '21 at 12:55
  • In the second example, there is not a declared heyHeyheyHey() function. when you call test() from route it has to throw error. But it's not throwing any error. – Taner Tunçer Oct 31 '21 at 13:01
  • Could you provide more details? What is your codes expected output? What is it actually outputting? What are you trying to achieve with this code? – Tyler2P Oct 31 '21 at 13:13
  • @TanerTunçer are you sure, the path to the test.js file is correct & you might have two test.js file at two different locations, & you're requiring the wrong one – Rahul Pal Oct 31 '21 at 13:14
  • you are not doing any error handling – Mohamed Oct 31 '21 at 15:29

1 Answers1

1

I ran a quick demo and the error has been thrown. I'm sure you've figured it out but here is my snippet. enter image description here

enter image description here

emre-ozgun
  • 676
  • 1
  • 7
  • 17