Search code examples
expressmiddleware

Express app.get not executing middleware function


I have an app.get call in my express.js app like this:

app.get('/blog', (query, res) => res.send('Hello Blog!'));

What I'm expecting to happen is that, every time someone goes to example.com/blog, function query is run, followed by res, which is just "Hello Blog!".

Further down the file, I have function query:

function query(req, res, next) {
  console.log('test');
  res.send('ploop!');
  next();
}

What I'm expecting here is to get something logged into my terminal window, followed by 'ploop!' getting logged to the window, and then next() should continue on with the original request. The /blog request goes through as expected, but console.log('test'); and res.send('ploop!); don't appear to execute.

I realize console.log must not be the best way to troubleshoot/debug this situation in Node/Express. Ultimately, I'm trying to produce some kind of obvious output from function query that will let me know that it's working (or not working).


Solution

  • Are you declaring app.use(query) after you initialize your express server?