Visual Studio Code - How To Debug Node.js Routes?

So I have my app structured like this:


I have my route defined in index.js like this:

module.exports = function (app) {
app.use('/emails', require('./routes/emails'));

And emails.js has a simple get route that looks like this:

router.get(/*'/emails */'/:id', function (req, res, next) {
   console.log('route hit');

I put breakpoints on each line (app.use, router.get and console.log). When I first launch my app, the breakpoints for app.use and router.get are hit. When I make my API call, my code executes but my breakpoint is never hit.

I'm assuming that this is some internal Node.js thing that I need to configure VS code for, but I'm not sure how to do that.

Does anyone know a way to get this to work? I really want to use routes and not have a massive server.js file, but not without the ability to debug with the IDE.



So I figured out what was happening... I'm posting in case someone else finds this problem. The solution was deceptively simple - moving this code in server.js:

var router = require('./router')(app);

to execute before I set up the server variable. I don't have a whole lot of time to dive into why this made a difference, but I hope it helps someone... order of endpoints in Express is very important!